当前位置: 移动技术网 > IT编程>开发语言>c# > 使用C#发送带附件的电子邮件的方法的代码示例分析

使用C#发送带附件的电子邮件的方法的代码示例分析

2019年07月18日  | 移动技术网IT编程  | 我要评论

以下代码除了实现发送简单的邮件以外,还包括了发送附件。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);
  }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网