public static bool SendMail(bool FlagAddAttachment)
{
//server address
string MailServer = "mail server address";
//port
int Port = 25;
//account
string MailAccount = "login account";
//pwd
string MailPassWord = "login password";
//senderFrom
System.Net.Mail.MailAddress SendFrom = new System.Net.Mail.MailAddress("mail address");
//senderTo
string SendTo = "mail address";
//cc
string SendCC = "mail address";
//title
string Subject = "mail title";
//body
string Body = "mail body";
try
{
//crate SmtpClient object && set SMTP/Port
System.Net.Mail.SmtpClient MySmtp = new System.Net.Mail.SmtpClient(MailServer, Port);
//set account/pwd
MySmtp.Credentials = new System.Net.NetworkCredential(MailAccount, MailPassWord);
//smtp using SSL
MySmtp.EnableSsl = true;
//set mail's Msg
System.Net.Mail.MailMessage MyMail = new System.Net.Mail.MailMessage();
//Set senderfrom
MyMail.From = SendFrom;
//Add senderto
MyMail.To.Add(SendTo);
//Add cc
MyMail.CC.Add(SendCC);
//Add title
MyMail.Subject = Subject;
//Add body
MyMail.Body = Body;
//Add Attachment
if (FlagAddAttachment == true)
{
string strFilePath = @"c:\test.txt";//your file path
System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(strFilePath);//add object
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;
MyMail.Attachments.Add(attachment1);
}
//Send mail
MySmtp.Send(MyMail);
return true;
}
catch (Exception e)
{
_ErrorMessage = e.Message;
return false;
}
}
星期三
c# System.Net.Mail 寄送信件、夾帶附件
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言