Mail.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using log4net;
using System.Configuration;
namespace MasterSoft.WinUI
{
/// <summary>
/// 发邮件模块
/// Author:tonyepaper.cnblogs.com
/// </summary>
public class Mail
{
private string senderAddress;
/// <summary>
/// 发件人
/// </summary>
public string SenderAddress
{
get { return senderAddress; }
set { senderAddress = value; }
}
private string receiverAddess;
/// <summary>
/// 收件人
/// </summary>
public string ReceiverAddess
{
get { return receiverAddess; }
set { receiverAddess = value; }
}
private string subject;
/// <summary>
/// 主题
/// </summary>
public string Subject
{
get { return subject; }
set { subject = value; }
}
private string body;
/// <summary>
/// 内容
/// </summary>
public string Body
{
get { return body; }
set { body = value; }
}
private string smtpHost;
/// <summary>
/// SMTP主机
/// </summary>
public string SmtpHost
{
get { return smtpHost; }
set { smtpHost = value; }
}
private int smtpPort;
/// <summary>
/// SMTP端口
/// </summary>
public int SmtpPort
{
get { return smtpPort; }
set { smtpPort = value; }
}
private string smtpPassword;
/// <summary>
/// SMTP密码
/// </summary>
public string Password
{
get { return smtpPassword; }
set { this.smtpPassword = value; }
}
/// <summary>
/// 从配置文件中读出SMTP相关信息
/// </summary>
public Mail()
{
senderAddress = ConfigurationManager.AppSettings["SmtpUser"];
smtpPassword = ConfigurationManager.AppSettings["SmtpPassword"];
smtpHost = ConfigurationManager.AppSettings["SmtpHost"];
smtpPort = Int32.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
}
/// <summary>
/// 邮件
/// </summary>
/// <param name="receiverAddess">收件人地址</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
public Mail(string receiverAddess, string subject, string body):this()
{
this.receiverAddess = receiverAddess;
this.subject = subject;
this.body = body;
}
/// <summary>
/// 发送邮件
/// </summary>
public bool Send()
{
MailMessage mailMessage = new MailMessage(senderAddress, receiverAddess);
mailMessage.Subject = subject;
mailMessage.Body = body;
SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort);
//使用SSL加密连线
smtpClient.EnableSsl=true;
NetworkCredential networkCredential = new NetworkCredential(senderAddress, smtpPassword);
smtpClient.Credentials = networkCredential;
try
{
smtpClient.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SmtpHost" value="smtp.gmail.com"/>
<add key="SmtpPort" value="587"/>
<add key="SmtpUser" value="tonyxxx@gmail.com"/>
<add key="SmtpPassword" value="*******"/>
</appSettings>
</configuration>
http://www.cnblogs.com/tonyepaper/archive/2008/12/21/1359205.html
另一种用法:
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;
namespace MyMail
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
private void button1_Click_1( object sender, EventArgs e)
{
// Mail Message Setting
string fromEmail = "XXX@gmail.com";
string fromName = "C.H.Huang";
MailAddress from = new MailAddress(fromEmail, fromName, Encoding.UTF8 );
string toEmail = "YYY@gmail.com";
MailMessage mail = new MailMessage(from, new MailAddress(toEmail) );
string subject = "Test Subject";
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
string body = "Test Body";
mail.Body = body;
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = false;
mail.Priority = MailPriority.High;
// SMTP Setting
SmtpClient client = new SmtpClient( );
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential( "username@gmail.com", "password" );
client.EnableSsl = true;
// Send Mail
client.SendAsync (mail, mail);
// Sent Compeleted Eevet
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
}
// Handle Sent Compeleted Eevet
private void client_SendCompleted( object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null )
{
MessageBox.Show (e.Error.ToString ( ) );
}
else
{
MessageBox.Show ( "Message sent." );
}
}
}
}