using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppEmailLibrary;
using System.Net.Mail;
using System.Net;
namespace WindowsFormTestEmail
{
public struct MailItem
{
public string To;
public string From;
public string Subject;
public string body;
public string userName;
public string password;
public string smtpHost;
}
public partial class Form1 : Form
{
WinHtmlControl.Editor edit;
public Form1()
{
InitializeComponent();
edit = new WinHtmlControl.Editor();
edit.Dock = DockStyle.Top;
edit.Height = 200;
this.Controls.Add(edit);
}
private void button1_Click(object sender, EventArgs e)
{
MailItem mi = new MailItem();
mi.From = "huangdongkui@tyicd.com";
mi.To = "huangdongkui@tyicd.com";
mi.body = edit.DocumentText;
mi.Subject = "test";
mi.smtpHost = "smtp.tyicd.com";
mi.password = "tyicd123";
mi.userName = "huangdongkui@tyicd.com";
try
{
Send(mi);
MessageBox.Show("发送成功");
}
catch (Exception)
{
MessageBox.Show("发送失败");
}
}
private void button2_Click(object sender, EventArgs e)
{
}
public void Send(MailItem aMailItem)
{
MailAddress from = new MailAddress(aMailItem.From);
MailAddress to = new MailAddress(aMailItem.To);
MailMessage message = new MailMessage(from, to);
message.Subject = aMailItem.Subject;
message.IsBodyHtml = true;
message.Body = aMailItem.body;
SmtpClient client = new SmtpClient(aMailItem.smtpHost);
client.Port = 25;
//设置发送邮件身份验证方式
//注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com
client.Credentials = new NetworkCredential(aMailItem.userName, aMailItem.password);
client.Send(message);
}
}
}