C#发送邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

/// <summary>
/// Summary description for sendMail
/// </summary>
///

namespace mailSystem
{
public class sendMail
{
public sendMail()
{
//
// TODO: Add constructor logic here
//

}

public bool send(String[] to, String subject, String body, String file)
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("ic@polyu.edu.hk", "IC General Office", System.Text.Encoding.UTF8);
// message.To.Add(new MailAddress("jianglei244@gmail.com"));
message.Subject = subject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
//message.CC.Add(new MailAddress("jianglei244@126.com"));
//message.Bcc.Add(new MailAddress("jianglei244@126.com"));
for (int i = 0; i < to.Length; i++)
{
message.Bcc.Add(to[i]);
}
message.Body = body;
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Priority = System.Net.Mail.MailPriority.High;
client.Host = "smtp.polyu.edu.hk";
client.Port = 25;
//由于是用的内网的邮件服务器,就没有用ssl,如果要免费的公共邮箱,一般要加上ssl,而且端口要改
// client.Credentials = new System.Net.NetworkCredential("ic@polyu.edu.hk", "general");
// client.EnableSsl = true;
//client.UseDefaultCredentials = true;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//object userState = message;
//client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

string[] files = file.Split(',');
for (int i = 0; i < files.Length; i++)
{
file = files[i];
if (file != "")
{
Attachment data = new Attachment(file);

//add marks
//ContentDisposition dispostion = data.ContentDisposition;
//dispostion.CreationDate = file.GetCreationTime(file);
//dispostion.ModificationDate = file.
//dispostion

message.Attachments.Add(data);
}
}

try
{
// userState = (userState == null) ? Guid.NewGuid() : userState;
// client.SendAsync(message, userState);
client.Send(message);
Console.WriteLine("Send Success!");
return true;

}
catch (System.Net.Mail.SmtpException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return false;
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值