使用 OpenSmtp.dll 发送邮件 (记录)

使用 OpenSmtp.dll 发送邮件关键代码
   
   
1 /// <summary>
2   /// 发送邮件XXXXXXXXXXX
3   /// </summary>
4   /// <param name="server"> 服务器 </param>
5   /// <param name="sender"> 寄件人 </param>
6   /// <param name="recipient"> 接收者 </param>
7 /// <param name="subject"> 主题(标题) </param>
8 /// <param name="body"> 正文 </param>
9 /// <param name="isBodyHtml"> bool 正文是否html格式 发送 </param>
10 /// <param name="encoding"> 字符编码 </param>
11 /// <param name="isAuthentication"> bool 是否验证发件人 </param>
12 /// <param name="files"> params string[] 应该是附件 </param>
13 public static void Send( string server, string sender, string recipient, string subject,
14 string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string [] files)
15 {
16 // 根据邮件服务器smtp服务器创建一个对象
17 SmtpClient smtpClient = new SmtpClient(server);
18 MailMessage message = new MailMessage(sender, recipient);
19 message.IsBodyHtml = isBodyHtml;
20
21 message.SubjectEncoding = encoding;
22 message.BodyEncoding = encoding;
23
24 message.Subject = subject;
25 message.Body = body;
26
27 message.Attachments.Clear(); // 清楚附件集合
28 if (files != null && files.Length != 0 )
29 {
30 for ( int i = 0 ; i < files.Length; ++ i)
31 {
32 Attachment attach = new Attachment(files[i]);
33 message.Attachments.Add(attach);
34 }
35 }
36 // 如果指示 验证发件人身份
37 if (isAuthentication == true )
38 {
39 smtpClient.Credentials = new NetworkCredential(
40 SmtpConfig.Create().SmtpSetting.User,
41 SmtpConfig.Create().SmtpSetting.Password);
42 }
43 // 将指定的右键发送到SMTP服务器以便传递
44 smtpClient.Send(message);
45 }

 

Smtp 配置文件 操作类
   
   
1 /// <summary>
2 /// Smtp 配置文件 操作类
3 /// </summary>
4 public class SmtpConfig
5 {
6 private static SmtpConfig _smtpConfig;
7 private string ConfigFile
8 {
9 get
10 {
11 string configPath = ConfigurationManager.AppSettings[ " SmtpConfigPath " ];
12 if ( string .IsNullOrEmpty(configPath) || configPath.Trim().Length == 0 )
13 {
14 configPath = HttpContext.Current.Request.MapPath( " ~/Config/SmtpSetting.config " );
15 }
16 else
17 {
18 if ( ! Path.IsPathRooted(configPath))
19 configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, " SmtpSetting.config " ));
20 else
21 configPath = Path.Combine(configPath, " SmtpSetting.config " );
22 }
23 return configPath;
24 }
25 }
26 /// <summary>
27 /// public SmtpSetting SmtpSetting
28 /// </summary>
29 public SmtpSetting SmtpSetting
30 {
31 get
32 {
33 XmlDocument doc = new XmlDocument();
34 doc.Load( this .ConfigFile);
35 SmtpSetting smtpSetting = new SmtpSetting();
36 smtpSetting.Server = doc.DocumentElement.SelectSingleNode( " Server " ).InnerText;
37 smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode( " Authentication " ).InnerText);
38 smtpSetting.User = doc.DocumentElement.SelectSingleNode( " User " ).InnerText;
39 smtpSetting.Password = doc.DocumentElement.SelectSingleNode( " Password " ).InnerText;
40 smtpSetting.Sender = doc.DocumentElement.SelectSingleNode( " Sender " ).InnerText;
41
42 return smtpSetting;
43 }
44 }
45 private SmtpConfig()
46 {
47
48 }
49 /// <summary>
50 /// public static SmtpConfig Create()
51 /// </summary>
52 /// <returns></returns>
53 public static SmtpConfig Create()
54 {
55 if (_smtpConfig == null )
56 {
57 _smtpConfig = new SmtpConfig();
58 }
59 return _smtpConfig;
60 }
61 }

 

SmtpSetting.config配置
   
   
1 <? xml version="1.0" encoding="utf-8" ?>
2 < configuration >
3 < Server > smtp服务器地址 </ Server >
4 < Authentication > true </ Authentication >
5 < User > 发信邮箱地址 </ User >
6 < Password > 发信密码 </ Password >
7 < Sender > 发信者,应该也是发信邮箱地址 </ Sender >
8 </ configuration >

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中有第三方库smtplib可以用于发送SMTP邮件,但smtplib库不支持通过socks代理服务器发送邮件。然而,我们可以使用第三方库pysocks来实现通过socks代理发送SMTP邮件。 要使用pysocks库发送SMTP邮件,我们需要首先安装pysocks库。可以使用pip命令进行安装,命令如下: ``` pip install pysocks ``` 安装好pysocks库后,我们可以使用以下代码来通过socks代理服务器发送SMTP邮件: ```python import socks import smtplib # 设置socks代理 socks.setdefaultproxy(socks.SOCKS5, "socks_proxy_ip", socks_proxy_port) socks.wrapmodule(smtplib) # 邮件内容 from_address = "sender@example.com" to_address = "recipient@example.com" subject = "This is the subject" body = "This is the body of the email" # 连接到smtp服务器并发送邮件 smtp_server = "smtp.example.com" smtp_port = 587 username = "your_username" password = "your_password" try: server = smtplib.SMTP(smtp_server, smtp_port) server.login(username, password) server.sendmail(from_address, to_address, f"Subject: {subject}\n\n{body}") server.quit() print("Email sent successfully!") except Exception as e: print(f"An error occurred while sending the email: {e}") ``` 在上述代码中,我们先设置了socks代理服务器的IP和端口,然后将smtplib模块包装到socks模块中。之后,我们可以使用smtplib库中的相关函数来连接到SMTP服务器并发送邮件。 同时,我们需要提供SMTP服务器的地址、端口以及发件人和收件人的地址。还需提供用户名和密码来进行SMTP服务器的身份验证。 注意:在运行上述代码之前,请确保您已经正确替换了代码中的占位符(如socks代理服务器的IP和端口、SMTP服务器的地址和端口、发件人和收件人的地址以及用户名和密码等)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值