110,535
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using Common;
//email用到的命名空间
using System.Collections;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace SendMail
{
class SendMail
{
public SendMail()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void DoSent(string AddressName, string email_s,string email_s_pass, StringBuilder sbLog, StringBuilder sbLogError, string total, string docu, string filepath)
{
string email_r = "";
email_r = conn.ExecSqlNonQuery("SELECT TOP 1 EMAIL FROM SYSUSER WHERE NAME = '" + AddressName + "'");
send(AddressName, email_r, email_s, email_s_pass, sbLogError, sbLog, total, docu, filepath);
}
private void send(string AddressName, string email_r, string email_s, string email_s_pass, StringBuilder sbLogError, StringBuilder sbLog, string total, string docu, string filepath)
{
string smtp_service = "****.*****.com"; //需要根据不同公司设定。发件人的smtp值。
if (IsEmail(email_r) == false)
{
sbLogError.Append("收件人" + AddressName + "邮件格式不对!");
return;
}
if (IsEmail(email_s) == false)
{
sbLogError.Append("发件人邮件格式不对!");
return;
}
CreateMessageWithAttachment(smtp_service, AddressName, email_r, email_s, email_s_pass, total, docu, filepath, sbLogError, sbLog);
}
//发邮件
private void CreateMessageWithAttachment(string server, string AddressName, string email_r, string email_s, string email_s_pass,string total, string docu, string filepath, StringBuilder sbLogError, StringBuilder sbLog)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
Attachment data = new Attachment(filepath, MediaTypeNames.Application.Octet);
try
{
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
email_s,//发件人
email_r,//收件人
total,//主题
docu //内容
);
// Create the file attachment for this e-mail message.
message.Attachments.Add(data);
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = new System.Net.NetworkCredential(email_s, email_s_pass);//用户名和密码
client.Send(message);
sbLog.Append("发送邮件至" + AddressName + "成功" + "\r\n");
}
catch
{
sbLogError.Append("发送邮件至" + AddressName + "失败" + "\r\n");
}
finally
{
data.Dispose();
}
}
//判断输出邮件地址格式
private static bool IsEmail(string Str)
{
return Regex.IsMatch(Str, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
}
}
}
//code1
public string GetStr(string inputStr,string reutrnstr)
{
}
//code2
public string getStr(string inputStr,string reutrnStr)
{
}