|
好像是发送出去了,但是收件邮箱完全收不到不知道什么原因哦- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Mail;
- using System.Web;
- namespace 练习8身份验证
- {
- public class Common
- {
- /// <summary>
- /// 发送电子邮件。
- /// </summary>
- /// <param name="strSmtpServer"></param>
- /// <param name="iSmtpPort"></param>
- /// <param name="Password"></param>
- /// <param name="strFrom"></param>
- /// <param name="strto"></param>
- /// <param name="strSubject"></param>
- /// <param name="strBody"></param>
- /// <returns></returns>
- public static bool SendMail(string strSmtpServer, int iSmtpPort, string Password, string strFrom, string strto, string strSubject, string strBody)
- {
- //设置发件人信箱,及显示名字
- MailAddress mailFrom = new MailAddress(strFrom);
- //设置收件人信箱,及显示名字
- MailAddress mailTo = new MailAddress(strto);
- //创建一个MailMessage对象
- MailMessage oMail = new MailMessage(mailFrom, mailTo);
- oMail.Subject = strSubject;
- oMail.Body = strBody;
- oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
- oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
- oMail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
- oMail.Priority = MailPriority.High;//设置邮件的优先级为高
- //发送邮件服务器
- SmtpClient client = new SmtpClient();
- //发送邮件服务器的smtp
- //每种邮箱都不一致
- client.Host = strSmtpServer; //指定邮件服务器
- //发送邮件服务器端口
- client.Port = iSmtpPort;
- ////设置超时时间
- //client.Timeout = 9999;
- //设置为发送认证消息
- client.UseDefaultCredentials = true;
- //指定服务器邮件,及密码
- //发邮件人的邮箱地址和密码
- client.EnableSsl = true;
- client.Credentials = new NetworkCredential(strFrom, Password);
- try//捕获异常
- {
- client.Send(oMail);
- //释放zy
- mailFrom = null;
- mailTo = null;
- client.Dispose();//释放zy
- oMail.Dispose(); //释放zy
- return true;
- }
- catch (Exception e)
- {
- //如果抛异常就将其异常信息返回
- Console.WriteLine(e.ToString());
- }
- return true;
- }
- }
- }
复制代码
|
|