1:发邮件功能如下:
a:批量添加邮箱
b:添加多个附件、并支持查看附件信息和移除附件。
2:因为免费,所以126,136服务器人家对咱 有控制:(我测试了126,136邮件)
1:邮箱虽已分组发送,但一次只能发送35个之内。
2:附件大小控件在50M。
3:功能采用c#的SmtpClient和MailMessage实现,核心源代码如下:
批量导入邮箱:
void SendsClick(object sender, EventArgs e)
{
portCount = 0;
textBox_addressee.Text = "";
this.openFile.Filter = "txt文件(*.txt)|*.txt";
if (this.openFile.ShowDialog() == DialogResult.OK)
{
string picFileName = this.openFile.FileName;
if(string.IsNullOrEmpty(picFileName))
return;
StreamReader sr = new StreamReader(picFileName,Encoding.Default);
string mail = sr.ReadToEnd();
string inpa = @"\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+";
MatchCollection collection = Regex.Matches(mail,inpa,RegexOptions.ExplicitCapture|RegexOptions.IgnoreCase);
foreach (Match m in collection) {
//if(!textBox_addressee.Text.Contains(m.Value)){
textBox_addressee.AppendText(m.Value+",");
portCount++;
//}
}
MessageBox.Show("共找到:"+collection.Count+" 可用邮件,已导入:"+portCount+" 位联系人");
}
}
添加附件:
void Button_choiceClick(object sender, EventArgs e)
{
this.openFile.Filter = "*.*|*.*";
if (this.openFile.ShowDialog() == DialogResult.OK)
{
string PicFileName = this.openFile.FileName;
if(!string.IsNullOrEmpty(PicFileName) && !user.Files.ContainsValue(PicFileName)){
fileLength += new FileInfo(PicFileName).Length;
if(fileLength>=(1024*1024*50))
{
MessageBox.Show("本次传输不能超过50M");
fileLength -= new FileInfo(PicFileName).Length;//还原
return;
}
comboBox_list.Items.Add(Path.GetFileName(PicFileName));
user.Files.Add(Path.GetFileName(PicFileName),PicFileName);
fileSize.Text = string.Format("已用{0}M,还剩下{1}M",fileLength/1024/1024,(49-(fileLength/1024/1024)));
}
}
}
分组邮件
public void runTask()
{
MailService mailService = new MailService();
String[] addresss = Regex.Split(user.Address,",");
//求出分组次数
IList array = new ArrayList();
StringBuilder sbTemp = null;
for (int y = 0; y < addresss.Length; y++) {
if(sbTemp==null)
{
sbTemp = new StringBuilder();
}
sbTemp.Append(addresss[y]).Append(",");
if(y!=0 && (y+1)%35==0){
array.Add(sbTemp);
sbTemp = null;
}
}
//剩下的
if(sbTemp!=null && sbTemp.ToString().Contains("@")){
array.Add(sbTemp);
}
MessageBox.Show("分"+array.Count+"组进行发送");
foreach (StringBuilder sb in array) {
user.Address = sb.ToString();
String tag = mailService.sendMail(user);
if(tag==massText.success)
{
isSendTrue = true;
errorMass.Text = massText.success;
errorMass.ForeColor = Color.LimeGreen;
}
else
{
errorMass.Text = tag;
errorMass.ForeColor = Color.Red;
}
}
}
发邮件:
public String sendMail(User user){
String tag = massText.success;
user.Username = user.Username.Replace(user.Server,"");
//服务器
SmtpClient sc = new SmtpClient();
sc.Host="smtp."+user.Server.Replace("@","");
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.UseDefaultCredentials = true;
sc.Credentials = new NetworkCredential(user.Username,user.Password);
//邮件内容
MailMessage message = new MailMessage();
message.From = new MailAddress(string.Format("{0}{1}",user.Username,user.Server));
try
{
string[] address = Regex.Split(user.Address+",",",");
for (int i = 0; i < address.Length; i++) {
if(!string.IsNullOrEmpty(address[i]))
{
if(!b){
message.To.Add(address[i]);
}else{
if(i%4==0)
message.CC.Add(address[i]);
else
message.Bcc.Add(address[i]);
}
}
}
}
catch(Exception)
{
return massText.mailTitle;
}
//附件
message.Subject = user.Title;
message.Body = user.Context;
Dictionary<String, String>.KeyCollection keyColl = user.Files.Keys;
foreach (String fileName in keyColl) {
string fileNamePath = user.Files[fileName];
message.Attachments.Add(new Attachment(fileNamePath));
}
//设置编码、发送级别
message.BodyEncoding = Encoding.GetEncoding("utf-8");
message.SubjectEncoding = Encoding.GetEncoding("utf-8");
message.IsBodyHtml = true;
message.Priority = MailPriority.Normal;
/*委托任务,出现等待页面
Wait wait = null;
((Action)delegate(){
wait = new Wait();
Thread.Sleep(1000);
this.Invoke((Action)delegate(){
try {
wait.ShowDialog();
Thread.Sleep(2000);
sc.Send(message);
} catch (Exception) {
tag = massText.failure;
}
});
}).BeginInvoke(new AsyncCallback(theout),wait);*/
sc.Send(message);
Thread.Sleep(1000);
message.Dispose();
sc.Dispose();
b = true;
return tag;
}
程序执行代码如下:
源代码下载地址:http://download.csdn.net/detail/u012892431/7120379 点击打开链接
原文:http://blog.csdn.net/hubiao_0618/article/details/22581123