邮件发送(单发)
代码
private void btnSend_Click(object sender, EventArgs e)
{
SettingsBiz settings = new SettingsBiz();
string smtp = settings.GetSettingsValue(FormEmailSettings.Mail_SMTP, Globals.CurrentSeatId);
string from = settings.GetSettingsValue(FormEmailSettings.Mail_From, Globals.CurrentSeatId);
string username = settings.GetSettingsValue(FormEmailSettings.Mail_UserName, Globals.CurrentSeatId);
string password = settings.GetSettingsValue(FormEmailSettings.Mail_Password, Globals.CurrentSeatId);
string title = txtTitle.Text.Trim();
string to = from;
string shoujianren = txtName.Text.Trim();//收件人
string body = txtContent.Text.Trim() ;
UsersTableAdapter adp = new UsersTableAdapter();
string realname = adp.GetDataByUserId(Globals.CurrentSeatId).Single().Realname;
MailMessage mailMsg = new MailMessage();//MailMessage mailMsg = new MailMessage();//两个类,别混了 引入System.Web这个Assembly
mailMsg.From = new MailAddress(from, "XXXXXX集团客服中心(" + realname + ")");//源邮件地址
mailMsg.To.Add(new MailAddress(to, shoujianren));//目的邮件地址。可以有多个收件人
mailMsg.Subject = title;//发送邮件的标题
mailMsg.Body = body;//发送邮件的内容
string filename = txtFujian.Text.Trim();
if (!string.IsNullOrEmpty(filename))
{
mailMsg.Attachments.Add(new Attachment(filename));//将附件附加到邮件消息对象中 ,可以有多个附件。
}
SmtpClient client = new SmtpClient(smtp);
client.Credentials = new NetworkCredential(from, password);
client.Send(mailMsg);
MessageBox.Show("发送成功");
}
{
SettingsBiz settings = new SettingsBiz();
string smtp = settings.GetSettingsValue(FormEmailSettings.Mail_SMTP, Globals.CurrentSeatId);
string from = settings.GetSettingsValue(FormEmailSettings.Mail_From, Globals.CurrentSeatId);
string username = settings.GetSettingsValue(FormEmailSettings.Mail_UserName, Globals.CurrentSeatId);
string password = settings.GetSettingsValue(FormEmailSettings.Mail_Password, Globals.CurrentSeatId);
string title = txtTitle.Text.Trim();
string to = from;
string shoujianren = txtName.Text.Trim();//收件人
string body = txtContent.Text.Trim() ;
UsersTableAdapter adp = new UsersTableAdapter();
string realname = adp.GetDataByUserId(Globals.CurrentSeatId).Single().Realname;
MailMessage mailMsg = new MailMessage();//MailMessage mailMsg = new MailMessage();//两个类,别混了 引入System.Web这个Assembly
mailMsg.From = new MailAddress(from, "XXXXXX集团客服中心(" + realname + ")");//源邮件地址
mailMsg.To.Add(new MailAddress(to, shoujianren));//目的邮件地址。可以有多个收件人
mailMsg.Subject = title;//发送邮件的标题
mailMsg.Body = body;//发送邮件的内容
string filename = txtFujian.Text.Trim();
if (!string.IsNullOrEmpty(filename))
{
mailMsg.Attachments.Add(new Attachment(filename));//将附件附加到邮件消息对象中 ,可以有多个附件。
}
SmtpClient client = new SmtpClient(smtp);
client.Credentials = new NetworkCredential(from, password);
client.Send(mailMsg);
MessageBox.Show("发送成功");
}