C#编程之发送带附件的电子邮件的方法的代码示例分析
白羽 2018-07-05 来源 :网络 阅读 1372 评论 0

摘要:本文将带你了解C#编程之发送带附件的电子邮件的方法的代码示例分析,希望本文对大家学C#/.Net有所帮助。


以下代码除了实现发送简单的邮件以外,还包括了发送附件。From图没有贴出,上面就两按钮,一个“添加附件”、一个“发送”。点击添加附件选择文件, 文件路径全存储在listbox1中。在发送按钮方法中,把listbox1所有的文件添加到mailmessage对象里作为邮件发送出去了,请看代码


   

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net.Mail;

using System.Net;

using System.Net.Security;

using System.IO;

using System.Net.Mime;

namespace SmtpTest

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

try

{

//定义一个mail对象

MailMessage mailmessage = new MailMessage(““, ““, “this is a test”, “yes!test!”);

//from email,to email,主题,邮件内容

mailmessage.Priority = MailPriority.Normal; //邮件优先级

SmtpClient smtpClient = new SmtpClient(“smtp.163.com”, 25); //smtp地址以及端口号

smtpClient.Credentials = new NetworkCredential(“ttlsa.com”, “xxxxxx”);//smtp用户名密码

smtpClient.EnableSsl = true; //启用ssl

//添加附件

Attachment attachment =null;

if(listBox1.Items.Count>0)

{

for (int i = 0; i < listBox1.Items.Count; i++)

{

string pathFileName = listBox1.Items[i].ToString();

string extName = Path.GetExtension(pathFileName).ToLower(); //获取扩展名

if(extName==”.rar”||extName==”.zip”) //.rar和.zip的文件属于压缩文件类型

{

attachment = new Attachment(pathFileName,MediaTypeNames.Application.Zip);

}else

{

attachment = new Attachment(pathFileName,MediaTypeNames.Application.Octet);

}

//设置附件的MIME信息

ContentDisposition cd = attachment.ContentDisposition;

cd.CreationDate = File.GetCreationTime(pathFileName);//设置附件的创建时间

cd.ModificationDate = File.GetLastWriteTime(pathFileName);//设置附件的修改时间

cd.ReadDate = File.GetLastAccessTime(pathFileName);//设置附件的访问时间

mailmessage.Attachments.Add(attachment);//将附件添加到mailmessage对象

}

}

smtpClient.Send(mailmessage);

MessageBox.Show(“发送成功”);

}

catch (SmtpException se)

{

MessageBox.Show(se.StatusCode.ToString());

}

}

//添加附件,把文件添加到listbox中

private void button2_Click(object sender, EventArgs e)

{

OpenFileDialog opd = new OpenFileDialog();//定义一个选择文件的对话框

opd.Multiselect = true;//允许选择多个文件

opd.CheckFileExists = true;//检查文件是否存在

opd.ValidateNames = true;//检查文件名的可用性

opd.ShowDialog();//打开对话框

if(opd.FileNames.Length>0)//将选择的文件路径写入listbox中

{

listBox1.Items.AddRange(opd.FileNames);

}

}

}

}

   


PS:利用网易163smtp邮箱发送邮件


   

protected void Button2_Click(object sender, EventArgs e)

  {

    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

    client.Host = "smtp.163.com";//使用163的SMTP服务器发送邮件

    client.UseDefaultCredentials = true;

    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

    client.Credentials = new System.Net.NetworkCredential("用户名", "密码");//163的SMTP服务器需要用163邮箱的用户名和密码作认证,如果没有需要去163申请个,                                    

    //这里假定你已经拥有了一个163邮箱的账户,用户名为abc,密码为*******

    System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();

    Message.From = new System.Net.Mail.MailAddress("上述用户名密码所对应的邮箱地址");//这里需要注意,163似乎有规定发信人的邮箱地址必须是163的,而且发信人的邮箱用户名必须和上面SMTP服务器认证时的用户名相同                               

    //因为上面用的用户名abc作SMTP服务器认证,所以这里发信人的邮箱地址也应该写为abc@163.com

    Message.To.Add("目标邮箱地址");//将邮件发送给Gmail

    //Message.To.Add("123456@qq.com");//将邮件发送给QQ邮箱

    Message.Subject = "customer feedback";

    Message.Body = "customer feedback content";

    Message.SubjectEncoding = System.Text.Encoding.UTF8;

    Message.BodyEncoding = System.Text.Encoding.UTF8;

    Message.Priority = System.Net.Mail.MailPriority.High;

    Message.IsBodyHtml = true;

    client.Send(Message);

  }


以上就介绍了C#.NET的相关知识,希望对C#.NET有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C#.NET频道!

   

 


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程