C#通过常用邮箱发送Email邮件
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 System.Net.Mail;
namespace EmailTest
{
public partial class MsEmail : Form
{
private SmtpClient smtpClient = new SmtpClient();
public MsEmail()
{
InitializeComponent();
combEmailServer.SelectedIndex = 0;
}
/// <summary>
/// 邮件发送测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("发送方邮箱地址");
message.From = fromAddress;
message.Subject = "报警邮件";
message.To.Add("目的方邮箱地址");
message.Body = "测试邮件";
if (combEmailServer.Text == "HOTMAIL")
{
smtpClient.Host = "smtp.live.com";
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "GMAIL")
{
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "QQ邮箱")
{
smtpClient.Host = "smtp.qq.com";
smtpClient.Port = 25;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "163邮箱")
{
smtpClient.Host = "smtp.163.com";
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "新浪邮箱")
{
smtpClient.Host = "smtp.sina.com";
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
}
smtpClient.Credentials = new System.Net.NetworkCredential("发送方邮箱地址", "发送方邮箱密码");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.SendAsync(message, combEmailServer.Text);
}
catch (Exception exm)
{
MessageBox.Show(exm.Message.ToString());
}
}
/// <summary>
/// 异步发送完毕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void smtpClient_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
String strEmailServer = (string)e.UserState;
if (e.Error != null)
{
MessageBox.Show(strEmailServer + "配置失败");
}
else
{
MessageBox.Show("发送成功");
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace EmailTest
{
public partial class MsEmail : Form
{
private SmtpClient smtpClient = new SmtpClient();
public MsEmail()
{
InitializeComponent();
combEmailServer.SelectedIndex = 0;
}
/// <summary>
/// 邮件发送测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSend_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("发送方邮箱地址");
message.From = fromAddress;
message.Subject = "报警邮件";
message.To.Add("目的方邮箱地址");
message.Body = "测试邮件";
if (combEmailServer.Text == "HOTMAIL")
{
smtpClient.Host = "smtp.live.com";
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "GMAIL")
{
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "QQ邮箱")
{
smtpClient.Host = "smtp.qq.com";
smtpClient.Port = 25;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "163邮箱")
{
smtpClient.Host = "smtp.163.com";
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
}
else if (combEmailServer.Text == "新浪邮箱")
{
smtpClient.Host = "smtp.sina.com";
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
}
smtpClient.Credentials = new System.Net.NetworkCredential("发送方邮箱地址", "发送方邮箱密码");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.SendAsync(message, combEmailServer.Text);
}
catch (Exception exm)
{
MessageBox.Show(exm.Message.ToString());
}
}
/// <summary>
/// 异步发送完毕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void smtpClient_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
String strEmailServer = (string)e.UserState;
if (e.Error != null)
{
MessageBox.Show(strEmailServer + "配置失败");
}
else
{
MessageBox.Show("发送成功");
}
}
}
}