C#如何利用SSL组Gmail包发邮件
public void Email(){string data = "";string CRLF = "rn";
TcpClient conn = new TcpClient("smtp.gmail.com", 465);sslStream = new SslStream(conn.GetStream(), false);sslStream.AuthenticateAsClient("smtp.gmail.com");strReader = new StreamReader(sslStream, Encoding.GetEncoding("gbk"));
string read = strReader.ReadLine();data = "HELO server" + CRLF;SendRequest(data);read = strReader.ReadLine();
data = "AUTH LOGIN" + CRLF;SendRequest(data);read = strReader.ReadLine();
data = EncodeBase64(txtUserName.Text.Trim()) + CRLF;SendRequest(data);read = strReader.ReadLine();
data = EncodeBase64(txtPassword.Text.Trim()) + CRLF;SendRequest(data);read = strReader.ReadLine();
data = "MAIL FROM: " + '<' + txtUserName.Text.Trim() + '>' + CRLF;SendRequest(data);read = strReader.ReadLine();
data = "RCPT TO: " + '<' + txtTo.Text.Trim() + '>' + CRLF;SendRequest(data);read = strReader.ReadLine();
data = "Data" + CRLF;
SendRequest(data);read = strReader.ReadLine();
if (read.StartsWith("354")){data = "MIME-Version: 1.0" + CRLF+ "From:" + txtNickname.Text + "<" + txtForm.Text + ">" + CRLF+ "To:" + txtTo.Text + CRLF+ "Message-Id: <4C204D28.1CBB7D.06708@m12-17.163.com>" + CRLF+ "Subject:" + txtSubject.Text + CRLF+ "Content-type:text/html;" + CRLF+ "Content-Transfer-Encoding:quoted-printable" + CRLF + CRLF+editor.BodyHtml + CRLF + CRLF+ "." + CRLF;
}SendRequest(data);read = strReader.ReadLine();}#endregion
#region 发送字节命令private void SendRequest(string stream){strData = Encoding.Default.GetBytes(stream.ToCharArray()); //字符串转为字节sslStream.Write(strData,0,strData.Length);}#endregion
送邮件需要SMTP服务器,可以使用Gmail的SMTP服务器,也可以使用IIS自带的SMTP服务,还可以使用第三方的SMTP服务器。如果使用Gmail,需要用Gmail的用户名密码作为认证信息,没有办法自定义发件人地址,总会显示为Gmail邮箱地址。并且必须使用SSL发送。下面是邮件发送代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Mail;
namespace WheatStudio.Utility.Mail{
public static class MailUtility{public class SmtpContext{public string Server { get; set; }public int Port { get; set; }public string UserName { get; set; }public string Password { get; set; }public bool EnableSSL { get; set; }}private static Encoding myEncoding;public static Encoding MyEncoding{get{if (myEncoding == null){myEncoding = Encoding.UTF8;}return myEncoding;}set{myEncoding = value;}}public static void SendMail(SmtpContext smtp, List<string> toAddressList, string fromAddress, string fromDisplayName, string title, string htmlBody){System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();foreach (string address in toAddressList){msg.To.Add(address);}msg.From = new MailAddress(fromAddress, fromDisplayName, MyEncoding);//发件人地址,发件人姓名,编码msg.Subject = title; //邮件标题msg.SubjectEncoding = MyEncoding;msg.Body = htmlBody; //邮件内容msg.BodyEncoding = MyEncoding;msg.IsBodyHtml = true;msg.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();client.Host = smtp.Server;if (smtp.Port != 0){client.Port = smtp.Port;//如果不指定端口,默认端口为25}if (!string.IsNullOrEmpty(smtp.UserName)){client.Credentials = new System.Net.NetworkCredential(smtp.UserName, smtp.Password);//向SMTP服务器提交认证信息}client.EnableSsl = smtp.EnableSSL;//默认为falseclient.Send(msg);}}}
下面是调用代码:
MailUtility.SmtpContext gmailSmtp = new MailUtility.SmtpContext(){Server = "smtp.gmail.com",UserName = "*****@gmail.com",Password = "********",EnableSSL = true //发送邮件 (SMTP) 服务器 - 需要 TLS2 或 SSL};MailUtility.SmtpContext mySmtp = new MailUtility.SmtpContext(){Server = "localhost"};//使用Gmail的SMTP服务器发送邮件,需要Gmail用户名密码作为认证信息,发件人地址会被强制转换为Gmail邮箱,必须使用SSL发送MailUtility.SendMail(gmailSmtp, new List<string> { "***@mapuni.com" }, "***@dreamarval.com", "发件人显示名", "测试邮件,使用Gmail发送", "<h4>Hellow mail!!<br /><a href='http://www.google.com.hk'>google</a></h4>");//使用自己配置的SMTP服务器,可以任意指定发件人地址MailUtility.SendMail(mySmtp, new List<string> { "***@mapuni.com" }, "***@dreamarval.com", "发件人显示名", "测试邮件,使用自己配置的SMTP服务器发送", "<h4>Hellow mail!!<br /><a href='http://www.google.com.hk'>google</a></h4>");
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(2)
public void Email()
{
string data = "";
string CRLF = "rn";
TcpClient conn = new TcpClient("smtp.gmail.com", 465);
sslStream = new SslStream(conn.GetStream(), false);
sslStream.AuthenticateAsClient("smtp.gmail.com");
strReader = new StreamReader(sslStream, Encoding.GetEncoding("gbk"));
string read = strReader.ReadLine();
data = "HELO server" + CRLF;
SendRequest(data);
read = strReader.ReadLine();
data = "AUTH LOGIN" + CRLF;
SendRequest(data);
read = strReader.ReadLine();
data = EncodeBase64(txtUserName.Text.Trim()) + CRLF;
SendRequest(data);
read = strReader.ReadLine();
data = EncodeBase64(txtPassword.Text.Trim()) + CRLF;
SendRequest(data);
read = strReader.ReadLine();
data = "MAIL FROM: " + '<' + txtUserName.Text.Trim() + '>' + CRLF;
SendRequest(data);
read = strReader.ReadLine();
data = "RCPT TO: " + '<' + txtTo.Text.Trim() + '>' + CRLF;
SendRequest(data);
read = strReader.ReadLine();
data = "Data" + CRLF;
SendRequest(data);
read = strReader.ReadLine();
if (read.StartsWith("354"))
{
data = "MIME-Version: 1.0" + CRLF
+ "From:" + txtNickname.Text + "<" + txtForm.Text + ">" + CRLF
+ "To:" + txtTo.Text + CRLF
+ "Message-Id: <4C204D28.1CBB7D.06708@m12-17.163.com>" + CRLF
+ "Subject:" + txtSubject.Text + CRLF
+ "Content-type:text/html;" + CRLF
+ "Content-Transfer-Encoding:quoted-printable" + CRLF + CRLF
+editor.BodyHtml + CRLF + CRLF
+ "." + CRLF;
}
SendRequest(data);
read = strReader.ReadLine();
}
#endregion
#region 发送字节命令
private void SendRequest(string stream)
{
strData = Encoding.Default.GetBytes(stream.ToCharArray()); //字符串转为字节
sslStream.Write(strData,0,strData.Length);
}
#endregion
送邮件需要SMTP服务器,可以使用Gmail的SMTP服务器,也可以使用IIS自带的SMTP服务,还可以使用第三方的SMTP服务器。
如果使用Gmail,需要用Gmail的用户名密码作为认证信息,没有办法自定义发件人地址,总会显示为Gmail邮箱地址。并且必须使用SSL发送。下面是邮件发送代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace WheatStudio.Utility.Mail
{
public static class MailUtility
{
public class SmtpContext
{
public string Server { get; set; }
public int Port { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public bool EnableSSL { get; set; }
}
private static Encoding myEncoding;
public static Encoding MyEncoding
{
get
{
if (myEncoding == null)
{
myEncoding = Encoding.UTF8;
}
return myEncoding;
}
set
{
myEncoding = value;
}
}
public static void SendMail(SmtpContext smtp, List<string> toAddressList, string fromAddress, string fromDisplayName, string title, string htmlBody)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
foreach (string address in toAddressList)
{
msg.To.Add(address);
}
msg.From = new MailAddress(fromAddress, fromDisplayName, MyEncoding);//发件人地址,发件人姓名,编码
msg.Subject = title; //邮件标题
msg.SubjectEncoding = MyEncoding;
msg.Body = htmlBody; //邮件内容
msg.BodyEncoding = MyEncoding;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
client.Host = smtp.Server;
if (smtp.Port != 0)
{
client.Port = smtp.Port;//如果不指定端口,默认端口为25
}
if (!string.IsNullOrEmpty(smtp.UserName))
{
client.Credentials = new System.Net.NetworkCredential(smtp.UserName, smtp.Password);//向SMTP服务器提交认证信息
}
client.EnableSsl = smtp.EnableSSL;//默认为false
client.Send(msg);
}
}
}
下面是调用代码:
MailUtility.SmtpContext gmailSmtp = new MailUtility.SmtpContext()
{
Server = "smtp.gmail.com",
UserName = "*****@gmail.com",
Password = "********",
EnableSSL = true //发送邮件 (SMTP) 服务器 - 需要 TLS2 或 SSL
};
MailUtility.SmtpContext mySmtp = new MailUtility.SmtpContext()
{
Server = "localhost"
};
//使用Gmail的SMTP服务器发送邮件,需要Gmail用户名密码作为认证信息,发件人地址会被强制转换为Gmail邮箱,必须使用SSL发送
MailUtility.SendMail(gmailSmtp, new List<string> { "***@mapuni.com" }, "***@dreamarval.com", "发件人显示名", "测试邮件,使用Gmail发送", "<h4>Hellow mail!!<br /><a href='http://www.google.com.hk'>google</a></h4>");
//使用自己配置的SMTP服务器,可以任意指定发件人地址
MailUtility.SendMail(mySmtp, new List<string> { "***@mapuni.com" }, "***@dreamarval.com", "发件人显示名", "测试邮件,使用自己配置的SMTP服务器发送", "<h4>Hellow mail!!<br /><a href='http://www.google.com.hk'>google</a></h4>");