C#实现发送QQ邮件并添加附件

您所在的位置:网站首页 打开qq邮箱需要什么应用 C#实现发送QQ邮件并添加附件

C#实现发送QQ邮件并添加附件

2024-07-11 18:58| 来源: 网络整理| 查看: 265

之前在公司遇到一个需求需要发送邮件并需要添加附件,我当时用了QQ的SMTP实现了发送邮件的功能。现在记录一下。并做了简单的封装。有不对的地方,请大家多多包涵,一起交流学习

一、准备工作, 要使用SMTP发送邮件,首先需要去QQ的邮箱配置。步骤如下 1、打开QQ邮箱 2、点击设置 在这里插入图片描述 3、点击账户 在这里插入图片描述 4、开启下图这两个服务,并拿到授权码,之后发送邮件的地方需要。 在这里插入图片描述

代码如下,已做了简单的封装

下面的邮件信息,我没有直接写在发送代码中,而是写在了配置文件web.config中,这样的好处是:在以后的维护中,需要修改邮件信息的话,直接修改配置文件就行,不用修改代码了,

//配置文件web.config //SMTP主机域 // 发送邮件的端口 //value填写你的QQ邮件 //value填写你的邮件的授权码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Mail; using System.Configuration; namespace EmailSend { public class EmailSender { //读取上面配置文件中的信息 string strMeEmail = ConfigurationManager.AppSettings.Get("MailSender");//邮件来自该邮件 string strPWD = ConfigurationManager.AppSettings.Get("MailSenderPwd"); //该邮件授权码 string strServer = ConfigurationManager.AppSettings.Get("MailSMTPServer"); //SMTP主机域 int strPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("MailSMTPPort")); //SMTP端口 MailMessage mailMsg; SmtpClient smtpClient; /// /// 构造函数 /// /// 邮件主题 /// 收件人 /// 邮件内容 public EmailSender(string strsubject,string strto,string strcontent) { mailMsg = new MailMessage(strMeEmail, strto); mailMsg.Subject = strsubject; mailMsg.SubjectEncoding = Encoding.UTF8; mailMsg.Priority = MailPriority.Normal; mailMsg.IsBodyHtml = true;//如果你的邮件内容需要拼接HTML的话,改属性设置为true mailMsg.Body = strcontent; mailMsg.BodyEncoding = Encoding.UTF8; } /// /// 增加附件 /// /// 附件路径 public void AddAttchment(string path) { try { Attachment attachMent; attachMent = new Attachment(path); mailMsg.Attachments.Add(attachMent); } catch(Exception ex) { throw new Exception(ex.Message); } } /// /// 邮件发送 /// /// 是否发送成功 public bool SendEmail() { smtpClient = new SmtpClient(strServer,strPort); smtpClient.UseDefaultCredentials = false; //是否使用默认身份验证(如果你的SMTP不需要身份验证,可以设置此项为TRUE,否则就为false),为false时,就需要身份验证,使用下面的Credentials smtpClient.EnableSsl=true; //是否加密链接,这里必须设置为true,否则无法发送邮件 smtpClient.DeliveryMethod= SmtpDeliveryMethod.Network; //指定发送邮件的方法,以网络的形式 smtpClient.Timeout=15000; smtpClient.Credentials = new NetworkCredential(strMeEmail,strPWD); //设置链接SMTP时的身份验证,也就是我要连接的QQ邮箱的服务器,我需要我邮箱的密码,才可以用它发送邮件,而且必须开通了QQ的SMTP服务,使用生成的凭证码 try { smtpClient.Send(mailMsg); return true; } catch(Exception ex) { throw new Exception(ex.Message); } } } }

调用 因为我的邮件内容是用html拼接的,所以是这样的,如果你不需要的话,就把 IsBodyHtml 设置为false,然后合同内容直接写就行。

StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append($"尊敬的XX先生/") stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); stringBuilder.Append(""); //DirectorEmail 是对方的邮件 EmailSender email = new EmailSender("你好", DirectorEmail, stringBuilder.ToString()); LogHelper.Info("DirectorEmail", "文件地址:" + ConlocalPath); //path 是附件地址 email.AddAttchment(Path); email.SendEmail();

注意: 1、因为QQ邮箱的SMTP使用了SSL加密,所以EnableSsl必须设置为true ,否则发送不出去. 2、增加附件的地方,给上文件的地址,比如这样添加附件 email.AddAttchment(@“C:\Users\Administrator\Downloads\图片\1.svg”); 3、发布到服务器上的时候,以华为云服务器为例,华为云服务器默认是关闭了25端口的,如果要使用25端口的话,要在华为云提交工单申请打开25端口 。

有问题请联系我,我看到了都会回复。谢谢大家。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3