c#写邮件发送程序
使用System.Net.Mail --------------------编程问答----------------------------------------编程问答-------------------- using System;
public bool SendMail(string reMail,string subject,string body)
{
bool isSend; //定义发送状态
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("test@foxmail.com"); //发件人邮箱
mailMessage.To.Add(reMail); //收件人邮箱
mailMessage.Subject = subject; //主题
mailMessage.Body = body; //正文
isSend = SetSmtpClient(mailMessage);
return isSend;
}
private bool SetSmtpClient(MailMessage mail)
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = false;
smtpClient.Host = "smtp.foxmail.com"; //smtp服务器地址
smtpClient.Port = 25; //端口号
smtpClient.Credentials = new NetworkCredential("用户名", "密码");
try
{
smtpClient.Send(mail);
return true;
}
catch
{
return false;
}
}
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Async
{
public class SimpleAsynchronousExample
{
static bool mailSent = false;
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
} else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}
public static void Main(string[] args)
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(args[0]);
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("jane@contoso.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("ben@contoso.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet,
// then cancel the pending operation.
if (answer.StartsWith("c") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");
}
}
} --------------------编程问答-------------------- 注册时发送到邮箱去
C# code
public void sendEmail(string email, string name)
{
try
{
jmail.Message jmessage = new jmail.Message();
jmessage.Charset = "GB2312";
jmessage.From = new functions().getInfoXX("t_Parameters", "p_name", "网站电子邮箱", "p_value").Trim();
// 发信地址
jmessage.MailServerUserName = new functions().getInfoXX("t_Parameters", "p_name", "网站电子邮箱", "p_value").Trim();
//smtp认证用户名(注:如为网易用户,不加要@163.com,只要前面部分即可)
jmessage.MailServerPassWord = new functions().getInfoXX("t_Parameters", "p_name", "网站电子邮箱密码", "p_value").Trim();
// smtp论证用户名密码
jmessage.FromName = "108人力银行";
// 发信人
jmessage.ReplyTo = new functions().getInfoXX("t_Parameters", "p_name", "网站电子邮箱", "p_value");
// 回复地址
jmessage.ContentType = "text/html";//邮件内容为html
jmessage.Subject = "您已成功注册108人力银行个人会员";
//string strbody = "<br>" + name + "您好!<br><br> 欢迎您注册108人力银行个人会员。<br><br><br><br> 108人力银行个人会员<br><br> "+DateTime.Now.Date;//邮件内容
string strbody = "";
strbody += new functions().getInfoXX("t_pagetexts", "p_name", "个人注册邮件反馈","p_value").Replace("$",name);
jmessage.HTMLBody = strbody;
// 邮件标题
jmessage.AddRecipient(email, "", "");
jmessage.Send("mail.108ren.com", false);//发送邮件smtp.163.com
jmessage.Close();//关闭对象,释放资源
}
catch (Exception err)
{
Response.Write(err);
}
使用方法是 this.sendEmail(email.Text.Trim(),username.Text.Trim());邮箱和用户名文本框
new functions().getInfoXX的方法是
/// table:表 /// bm:where条件中的数据表字段名称/// bmvalue:where 条件中的数据表字段内容
/// tname:需要查询的表字段名称
public string getInfoXX(string table, string bm, string bmvalue, string tname)
{
DBconn DB = new DBconn();
string bh = DB.getExecuteString("select " + tname + " from " + table + " where " + bm + "='" + bmvalue + "'");
return bh;
}
下面的是找回密码时发送到用户邮箱代码使用方法和上面的一样
C# code
public void sendEmail(string email, string name)
{
try
{
jmail.Message jmessage = new jmail.Message();
jmessage.Charset = "GB2312";
jmessage.From = "86085005@163.com";
// 发信地址
jmessage.MailServerUserName = "86085005";
//smtp认证用户名(注:如为网易用户,不加要@163.com,只要前面部分即可)
jmessage.MailServerPassWord = "kingseer";
// smtp论证用户名密码
jmessage.FromName = "108人力银行";
// 发信人
jmessage.ReplyTo = "86085005@163.com";
// 回复地址
jmessage.ContentType = "text/html";//邮件内容为html
jmessage.Subject = "108人力银行临时密码";
string strbody = "";
strbody += new functions().getInfoXX("t_pagetexts", "p_name", "找回密码邮件反馈-个人", "p_value").Replace("$", name).Replace("@%", lspass);
jmessage.HTMLBody = strbody;
// 邮件标题
jmessage.AddRecipient(email, "", "");
jmessage.Send("smtp.163.com", false);//发送邮件smtp.163.com
jmessage.Close();//关闭对象,释放资源
}
catch (Exception err)
{
Response.Write(err);
}
}
--------------------编程问答-------------------- 没发错吧。哎发错了 --------------------编程问答-------------------- 首先这个代码是我从自己开发的一个邮件收发项目中直接剪切出来的,客户使用了一年多了,完全没有问题.客户是自己架设的邮件服务器..如果有问题可以加我的qq 13160096
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.IO;
using System.Collections;
using System.Threading;
private void sendmail()
{
myMail.From = new MailAddress(regrw.StrDecryp(regrw.RegRead("popusername")) + "@XXXX.cn", sendname, Encoding.GetEncoding(936));
//myMail.To.Add(new MailAddress("ccc@XXXX.cn"));
//myMail.CC.Add(new MailAddress("mstzyd@XXXX.cn"));
myMail.Subject = "fsr\\"+TitleTextBox.Text;
myMail.Body = BodyRichTextBox.Text;
SmtpClient smtp = new SmtpClient("mail.XXXX.cn");
smtp.UseDefaultCredentials = true;
myMail.SubjectEncoding = Encoding.UTF8;
myMail.BodyEncoding = Encoding.UTF8;
try
{
smtp.Send(myMail);
SendToSql();
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送失败");
}
--------------------编程问答-------------------- 首先这个代码是我从自己开发的一个邮件收发项目中直接剪切出来的,客户使用了一年多了,完全没有问题.客户是自己架设的邮件服务器..如果有问题可以加我的qq 13160096
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.IO;
using System.Collections;
using System.Threading;
private void sendmail()
{
myMail.From = new MailAddress(regrw.StrDecryp(regrw.RegRead("popusername")) + "@XXXX.cn", sendname, Encoding.GetEncoding(936));
//myMail.To.Add(new MailAddress("ccc@XXXX.cn"));
//myMail.CC.Add(new MailAddress("mstzyd@XXXX.cn"));
myMail.Subject = "fsr\\"+TitleTextBox.Text;
myMail.Body = BodyRichTextBox.Text;
SmtpClient smtp = new SmtpClient("mail.XXXX.cn");
smtp.UseDefaultCredentials = true;
myMail.SubjectEncoding = Encoding.UTF8;
myMail.BodyEncoding = Encoding.UTF8;
try
{
smtp.Send(myMail);
SendToSql();
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送失败");
}
--------------------编程问答-------------------- 首先这个代码是我从自己开发的一个邮件收发项目中直接剪切出来的,客户使用了一年多了,完全没有问题.客户是自己架设的邮件服务器..如果有问题可以加我的qq 13160096
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.IO;
using System.Collections;
using System.Threading;
private void sendmail()
{
myMail.From = new MailAddress(regrw.StrDecryp(regrw.RegRead("popusername")) + "@XXXX.cn", sendname, Encoding.GetEncoding(936));
//myMail.To.Add(new MailAddress("ccc@XXXX.cn"));
//myMail.CC.Add(new MailAddress("mstzyd@XXXX.cn"));
myMail.Subject = "fsr\\"+TitleTextBox.Text;
myMail.Body = BodyRichTextBox.Text;
SmtpClient smtp = new SmtpClient("mail.XXXX.cn");
smtp.UseDefaultCredentials = true;
myMail.SubjectEncoding = Encoding.UTF8;
myMail.BodyEncoding = Encoding.UTF8;
try
{
smtp.Send(myMail);
SendToSql();
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送失败");
}
--------------------编程问答-------------------- 首先这个代码是我从自己开发的一个邮件收发项目中直接剪切出来的,客户使用了一年多了,完全没有问题.客户是自己架设的邮件服务器..如果有问题可以加我的qq 13160096
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.IO;
using System.Collections;
using System.Threading;
private void sendmail()
{
myMail.From = new MailAddress(regrw.StrDecryp(regrw.RegRead("popusername")) + "@XXXX.cn", sendname, Encoding.GetEncoding(936));
//myMail.To.Add(new MailAddress("ccc@XXXX.cn"));
//myMail.CC.Add(new MailAddress("mstzyd@XXXX.cn"));
myMail.Subject = "fsr\\"+TitleTextBox.Text;
myMail.Body = BodyRichTextBox.Text;
SmtpClient smtp = new SmtpClient("mail.XXXX.cn");
smtp.UseDefaultCredentials = true;
myMail.SubjectEncoding = Encoding.UTF8;
myMail.BodyEncoding = Encoding.UTF8;
try
{
smtp.Send(myMail);
SendToSql();
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送失败");
}
--------------------编程问答-------------------- 首先声明,该代码是从我自己的项目中剪切出来的.客户已使用1年多..客户是自己架设的邮件服务器.
如果有什么问题可以跟我联系,qq:13160096
--------------------编程问答-------------------- 补充一下,
private Attachment AddAtt(string filepath)
{
Attachment att = new Attachment(filepath);
att.NameEncoding = Encoding.GetEncoding("gb2312");
return att;
}
private void sendmail()
{
pictureBox1.Visible = true;
label2.Visible = true;
ButtonSend.Enabled = false;
myMail.From = new MailAddress(regrw.StrDecryp(regrw.RegRead("popusername")) + "@xxxx.cn", sendname, Encoding.GetEncoding(936));
//myMail.To.Add(new MailAddress("ccc@xxxx.cn"));
//myMail.CC.Add(new MailAddress("mstzyd@xxxx.cn"));
myMail.Subject = TitleTextBox.Text;
myMail.Body = BodyRichTextBox.Text;
SmtpClient smtp = new SmtpClient("mail.xxxx.cn");
smtp.UseDefaultCredentials = true;
myMail.SubjectEncoding = Encoding.GetEncoding("GBK");
myMail.BodyEncoding = Encoding.GetEncoding("GBK");
try
{
smtp.Send(myMail);
SendToSql();
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送失败");
}
finally
{
if (MessageBox.Show("邮件发送完毕,现在关闭窗口吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
//TitleTextBox.Text = "";
//BodyRichTextBox.Text = "";
//myMail.Attachments.Clear();
//listBoxAtt.Items.Clear();
this.Close();
}
else
{
ButtonSend.Enabled = true;
listBoxSender.Items.Clear();
myMail.CC.Clear();
pictureBox1.Visible = false;
label2.Visible = false;
}
}
}
记得引用
system.net.mail命名空间 --------------------编程问答-------------------- 用jmail吧! --------------------编程问答-------------------- private void btnSend_ServerClick(object sender, System.EventArgs e)
{
if (logic.UserIsExist(this.txtUserId.Text,this.txtTel3.Text,this.txtTel2.Text,this.txtTel1.Text))
{
DataTable dt = logic.GetUserPassword(this.txtUserId.Text,this.txtTel3.Text,this.txtTel2.Text,this.txtTel1.Text);
string pwd = logic.GetNewPassWord();//dt.Rows[0]["PASSWORD"].ToString().TrimEnd();
string userId = dt.Rows[0]["PERSON_NO"].ToString().TrimEnd();
string email = dt.Rows[0]["PERSON_EMAIL"].ToString().TrimEnd();
string meaNing = logic.GetMeaning();
string mailTitle = logic.GetMailTitle();
string mailPassword = "password";
MailMessage mailMessage = new MailMessage();
mailMessage.To = email;//"對方郵箱";
mailMessage.From = "SERVICE@MAIL.PEC.COM.TW";
mailMessage.Subject = mailTitle;//"山立通運使用者密易做图重送";
//mailMessage.Attachments.Add(new MailAttachment(""));//發送附件
mailMessage.BodyFormat = MailFormat.Html;
//mailMessage.BodyEncoding = Encoding.GetEncoding("GB2312");
mailMessage.Body = "帳號:"+userId+"密易做图為:" + pwd + ","+meaNing+"";//註:「請記住您的密易做图並小心保管以免洩露及遺失.若有任何問題請聯絡資訊部 電話:04-xxxxxxxx」;
//"賬號:"+userId+
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userId);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassword);
SmtpMail.SmtpServer = "mail.pec.com.tw";//SERVICE@MAIL.PEC.COM.TW//127.0.0.1
SmtpMail.Send(mailMessage);
this.Msg.AlertMessage = "發送成功!";
logic.ChangePwd(userId, pwd);//將資料表中舊pwd更新
ClearAction();
}
else
{
this.Msg.AlertMessage = "賬號或電話號易做图錯誤,請重新輸入!";
ClearAction();
}
}
补充:.NET技术 , C#