using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
namespace Portal_Utility.Email
{
public class SendEmail
{
public bool SendWebEmail(string sToMailUser, string sSubject, string sBody)
{
if (sToMailUser == "")
{
return false;
}
string MailServerPort = "";
string MailServerHost = "";
// string MailServerMailID = "GM";
string MailServerPassWord = "";
string sFromUserName = "";//
string sFromUserDisplayName = "";//顯示名稱
MailServerPort = "25";//服務端口號
MailServerHost = "smtp.ym.163.com";
//MailServerMailID = "GM";
MailServerPassWord = "cso4games#@!";
sFromUserName = "[email protected]";
sFromUserDisplayName = "[email protected]";
//System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage(MailServerUserName, ToMailID); 對於部分的郵箱這種方式是不行的,不知道爲什麼
System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
myEmail.From = new MailAddress(sFromUserName, sFromUserDisplayName);
myEmail.To.Add(sToMailUser);
myEmail.Subject = sSubject;
myEmail.Body = sBody;
myEmail.Priority = MailPriority.Normal;
myEmail.IsBodyHtml = true; //郵件形式,.Text、.Html
System.Net.Mail.SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = MailServerHost;
smtpclient.Port = Convert.ToInt32(MailServerPort);
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(sFromUserName, MailServerPassWord);
smtpclient.Timeout = 1000 * 100;
try
{
smtpclient.Send(myEmail);
return true;
}
catch (Exception e)
{
Console.WriteLine("{0} Second exception.", e.Message);
return false;
}
}
public bool SendWebEmailAttachment(string sToMailUser, string sSubject, string sBody, string AttachmentPath)
{
string MailServerPort = "25";
string MailServerHost = "smtp.ym.163.com";
string MailServerMailID = "CS";
string MailServerPassWord = "cso4games#@!";
string sFromUserName = "[email protected]";//郵件地址[email protected] ;密碼gm365qaz
string sFromUserDisplayName = "[email protected]";//顯示名稱
//System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage(MailServerUserName, ToMailID); 對於部分的郵箱這種方式是不行的,不知道爲什麼
System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
myEmail.From = new MailAddress(sFromUserName, sFromUserDisplayName);
myEmail.To.Add(sToMailUser);
myEmail.Subject = sSubject;
myEmail.Body = sBody;
myEmail.Priority = MailPriority.Normal;
myEmail.IsBodyHtml = true; //郵件形式,.Text、.Html
//附件
string strFilePath = AttachmentPath;
System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(strFilePath);//添加附件
attachment1.Name = System.IO.Path.GetFileName(strFilePath);
attachment1.NameEncoding = System.Text.Encoding.GetEncoding("gb2312");
attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
attachment1.ContentDisposition.Inline = true;
attachment1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = attachment1.ContentId;//關鍵性的地方,這裏得到一個id數值
myEmail.Attachments.Add(attachment1);
System.Net.Mail.SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = MailServerHost;
smtpclient.Port = Convert.ToInt32(MailServerPort);
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(MailServerMailID, MailServerPassWord);
smtpclient.Timeout = 1000 * 100;
try
{
smtpclient.Send(myEmail);
return true;
}
catch
{
return false;
}
}
}
}
以上部分爲發送郵件的一個類,直接建這個類就好了
調用此類中定義該方法的地方
Portal_Utility.Email.SendEmail sendmail = new Portal_Utility.Email.SendEmail();
bool falg = false;
string sEmailInfo = "From:www.o4games.com <br>";
string time=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
sEmailInfo += "親愛的用戶您好:您的賬號爲 :" + Account + "<br>" + " 正在申請修改密碼。請點擊鏈接<a href=' http://localhost:5843/CheckUrl.aspx?Semail=" + Semail + "&&Account=" + Account + "&&CurrentTime=" + time + "'>www.o4games.com.</a>" + "<br>" + "進行下一步操作";
sEmailInfo+="該地址有效期爲20分鐘,請您在20分鐘以內點擊該鏈接。";
string sEmailTitle = "尊敬的" + Account + "用戶,您正在進行修改您的密碼,請謹慎操作。";
falg = sendmail.SendWebEmail(Semail, sEmailTitle, sEmailInfo);