邮件发送:
public static bool Email(string mailtitle, string mailcontent, string MailTo)
{
try
{
jmail.Message Jmail = new jmail.Message();
String Subject = mailtitle;
String body = mailcontent;
String FromEmail = ConfigurationManager.AppSettings["mailName"];
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent = true;
//Jmail创建的日志,前提loging属性设置为true
Jmail.Logging = true;
//字符集,缺省为"US-ASCII"
Jmail.Charset = "GB2312";
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/HTML"即可。
Jmail.ContentType = "text/HTML";
//添加收件人
string[] arraymail = MailTo.Split(';');
for (int i = 0; i < arraymail.Length; i++)
{
Jmail.AddRecipientBCC(arraymail[i], "");
}
Jmail.From = FromEmail;
//发件人邮件用户名
Jmail.MailServerUserName = ConfigurationManager.AppSettings["mailUser"];
//发件人邮件密码
Jmail.MailServerPassWord = ConfigurationManager.AppSettings["mailPwd"];
//设置邮件标题
Jmail.Subject = Subject;
//邮件内容
Jmail.Body = body;
//Jmail发送的方法
Jmail.Send(ConfigurationManager.AppSettings["smtp"], false);
Jmail.Close();
return true;
}
catch
{
return false;
}
}
邮件发送和接收
public void ReceiveEmail()
{
try
{
// 创建接受邮件协议对象
POP3Class pop3 = new POP3Class();
// 连接远程邮件
pop3.Connect("2232282010@qq.com", "*****", "pop.qq.com", 110);
if (pop3.Count > 0)
{
//得到所有邮件
Response.Write("邮件总数:" + pop3.Count+"<br>");
Console.WriteLine();
//注意 Messages 集合是从 1 开始编号的,
//而不是像多数集合或者数组那样从 0 开始编号。
//这是按照国际 POP3 服务器的保存规范设置的。
for (int i = 1; i <= pop3.Count; i++)
{
MessageClass m = (MessageClass)pop3.DownloadSingleMessage(i);
Response.Write(i.ToString()+": 发件人"+m.From + "<br>");
Response.Write("标题: "+m.Subject + "<br>");
Response.Write("内容: "+m.Body+"<br>");
Response.Write("附件数:"+m.Attachments.Count+"<br>");
// 得到当前邮件的附件信息
for (int j = 0; j < m.Attachments.Count; j++)
{
string name = m.Attachments[j].Name;
Response.Write("附件名:"+name+"<br>");
m.Attachments[j].SaveToFile(Server.MapPath(name));
}
}
}
// 手动删除邮件
// pop3.DeleteMessages();
pop3.Disconnect();
pop3 = null;
}
catch (Exception e)
{
Response.Write(e.Message);
Response.Write("请检查邮件服务器设置是否正确");
}
}
// 利用Jmail发送邮件
public void SendEmail(string title,string content)
{
// 建立发邮件类
MessageClass mail = new MessageClass();
//mail.AddRecipient("danrandom@sina.com", "", "");
mail.AddRecipient("894145261@qq.com", "", "");
//string[] sendTo = { "894145261@qq.com", "775759699@qq.com", "594837637@qq.com" };
//for (int i = 0; i < sendTo.Length; i++)
//{
// mail.AddRecipient(sendTo[i], "", "");
//}
// '屏蔽例外错误,返回FALSE跟TRUE两值
mail.Silent = true;
// 字符集
mail.Charset = "gb2312";
//Encoding : 字符串 设置附件编码方式(缺省是"base64)。 可以选择使用的是"base64", "uuencode" or "quoted-printable"
// 附件编码格式
mail.Encoding = "BASE64";
mail.Logging = true;//启用邮件日志
// 若添加附件需要注释这句 不然会出现乱码
//mail.ContentType = "text/html";
// 是否将信头编码成iso-8859-1
mail.ISOEncodeHeaders = false;
// 优先级
mail.Priority = Convert.ToByte(1);
// 发送人邮件地址
mail.From = "2232282010@qq.com";
// 发送人姓名
mail.FromName = "**";
// 身份验证用户名
mail.MailServerUserName = "2232282010@qq.com";
// 用户密码
mail.MailServerPassWord = "****";
// 邮件标题
mail.Subject = title;
mail.Body = content;
//mail.HTMLBody = content;
if (mail.Send("smtp.qq.com", false))
{
Response.Write("发送成功!");
}
else
{
Response.Write("发送失败,请检查邮件服务器的设置!");
}
mail.ClearRecipients();
// 强制JMail关闭缓冲的与邮件服务器的连接
mail.Close();
}