c#邮件发送代码
原创
©著作权归作者所有:来自51CTO博客作者goot2012的原创作品,如需转载,请与作者联系,否则将追究法律责任
string _toEmail,_subject,_body,_fromEmail,_fromPassword;
_toEmail = "goot2012@live.com";//获取收件人邮箱
_subject = "你好,我是";//获取邮件主题
_body = "你好! 你吃了饭没!";//获取邮件正文
_fromEmail = "xxxxxx@qq.com";//获取发件人邮箱
_fromPassword = "xxxxxxxxx";//获取发件人密码
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.qq.com");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(_fromEmail, _fromPassword);//设置发件人的邮箱和密码
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//以SMTP形式发送邮件
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(_toEmail);//设置收件人的邮箱
// message.To.Add("503164193@qq.com");
message.From = new System.Net.Mail.MailAddress(_fromEmail);//设置发件人的邮箱和别名
message.Subject = _subject;//设置邮件主题
message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8");//主题编码
message.Body = _body;//设置邮件正文
message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");//正文编码
message.IsBodyHtml = true;//是否以HTML发送
//附件
string strFilePath = @"E:\logo.jpg";
System.Net.Mail.Attachment p_w_upload1 = new System.Net.Mail.Attachment(strFilePath);//添加附件
p_w_upload1.Name = System.IO.Path.GetFileName(strFilePath);
p_w_upload1.NameEncoding = System.Text.Encoding.GetEncoding("gb2312");
p_w_upload1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
p_w_upload1.ContentDisposition.Inline = true;
p_w_upload1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = p_w_upload1.ContentId;//关键性的地方,这里得到一个id数值
message.Attachments.Add(p_w_upload1);
//string name = System.IO.Path.GetFileName(FileUpload1.FileName);
//if (string.IsNullOrEmpty(name) && name != "")
// message.Attachments.Add(new System.Net.Mail.Attachment(name));
try{
client.Send(message);//发送邮件
}
catch(Exception e)
{=-
MessageBox.Show("邮件发送失败!{0}",e.Message);
}
//if(System.IO.File.Exists(name))
//System.IO.File.Delete(name);
上一篇:css+div导航的制作
下一篇:聚合函数+模糊查询
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C#发送邮件...
关键词:C#发送邮件 发邮件 发送邮件因程序的需要,从网上参考了下别人的代码
System 发送邮件 Network -
C#,WinForm发送邮件
C#,WinForm发送邮件
C# WinForm发送邮件 -
使用C#发送邮件
最近有用户提出了一个新的需求,希望公司
c# string 服务器 电话 Text -
c#使用FluentEmail发送邮件
在实际的项目开发中,我们会遇到很多需要通过程序发送邮件的场景,比如邮可以愉快的写代码了,它的编码使用方式非常简单而且非常简洁,下面
c# 开发语言 服务器 发送邮件 github