public class E_mail
{
public static string Fa(string a, string b, string c, string d)
{
MailMessage mail = new MailMessage(b,a);
mail.Subject = c;
mail.Body = d;
SmtpClient smtp = new SmtpClient("smtp.163.com");
smtp.Credentials = new temp();
smtp.Send(mail);
return "ads";
}
}
internal class temp : ICredentialsByHost
{
#region ICredentialsByHost 成员 public NetworkCredential GetCredential(string host, int port, string authenticationType)
{
return new NetworkCredential("[email protected]", "ASDF123456"); //或者用你的163信箱的用户名和密码
} #endregion
}return new NetworkCredential("[email protected]", "ASDF123456"); 这里出错说是没有邮箱
{
public static string Fa(string a, string b, string c, string d)
{
MailMessage mail = new MailMessage(b,a);
mail.Subject = c;
mail.Body = d;
SmtpClient smtp = new SmtpClient("smtp.163.com");
smtp.Credentials = new temp();
smtp.Send(mail);
return "ads";
}
}
internal class temp : ICredentialsByHost
{
#region ICredentialsByHost 成员 public NetworkCredential GetCredential(string host, int port, string authenticationType)
{
return new NetworkCredential("[email protected]", "ASDF123456"); //或者用你的163信箱的用户名和密码
} #endregion
}return new NetworkCredential("[email protected]", "ASDF123456"); 这里出错说是没有邮箱
/// 发送邮件
/// </summary>
/// <param name="strSmtpServer">smtp地址</param>
/// <param name="UserName">用户名</param>
/// <param name="Password">密码</param>
/// <param name="strFrom">发信人地址</param>
/// <param name="strto">收信人地址</param>
/// <param name="strSubject">邮件标题</param>
/// <param name="strBody">邮件正文</param>
public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
{
//生成一个 使用SMTP发送邮件的客户端对象
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer); //表示以当前登录用户的默认凭据进行身份验证
client.UseDefaultCredentials = true; //包含用户名和密码
client.Credentials = new System.Net.NetworkCredential(UserName, Password); //指定如何发送电子邮件。
//Network 电子邮件通过网络发送到 SMTP 服务器。
//PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。
//SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; //建立邮件对象
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject,strBody);
//定义邮件正文,主题的编码方式
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
//获取或设置一个值,该值指示电子邮件正文是否为 HTML。
message.IsBodyHtml = false;
//指定邮件优先级
message.Priority = System.Net.Mail.MailPriority.Normal; //添加附件
//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@"f:/baihe.txt");
if (strFileName != "" && strFileName != null)
{
Attachment data = new Attachment(strFileName);
message.Attachments.Add(data);
}
//发件人身份验证,否则163 发不了
client.Credentials = new System.Net.NetworkCredential(strFrom, Password); //发送
client.Send(message);
}
楼主这样些很麻烦
internal class temp : ICredentialsByHost
{
#region ICredentialsByHost 成员 public NetworkCredential GetCredential(string host, int port, string authenticationType)
{
return new NetworkCredential("[email protected]", "ASDF123456"); //或者用你的163信箱的用户名和密码
} #endregion
} return new NetworkCredential("[email protected]", "ASDF123456"); 这里出错说是没有邮箱 把这里换成 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer); client.Credentials = new System.Net.NetworkCredential(邮箱地址, 邮箱密码);
{
jmail.MessageClass oJmailMessage = new jmail.MessageClass();//建立发邮件类
DataTable dt = new DataTable();
MainBF mail = new MainBF();
string User_id = Session["Staff_ID"].ToString();
string UserName = Convert.ToString(Session["StaffName"]);
dt = mail.GetAllMailSeting(User_id); if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//邮箱地址,用户名,密码
string userEmail = dt.Rows[i]["MailAddress"].ToString();
string userName = dt.Rows[i]["UserName"].ToString();
string userPwd = dt.Rows[i]["PassWord"].ToString();
string smtpServer = dt.Rows[i]["SmtpSever"].ToString();
try
{
oJmailMessage.Charset = "GB2312";// 字符集
oJmailMessage.ContentTransferEncoding = "BASE64";
oJmailMessage.Encoding = "BASE64";//附件的编码格式
oJmailMessage.Priority = Convert.ToByte(MailImportance);
//oJmailMessage.ContentType = "text/html";
oJmailMessage.ISOEncodeHeaders = false;//是否将信头编码成iso-8859-1字符集
oJmailMessage.Priority = Convert.ToByte(listImportance.SelectedValue); oJmailMessage.From = dt.Rows[i]["MailAddress"].ToString();//发送人邮件地址
oJmailMessage.FromName = UserName;//发送人姓名
oJmailMessage.Subject = txtSubject.Text.Trim();// 邮件主题
oJmailMessage.MailServerUserName = dt.Rows[i]["UserName"].ToString();//身份验证的用户名
oJmailMessage.MailServerPassWord = dt.Rows[i]["PassWord"].ToString();//用户密码 oJmailMessage.HTMLBody = txtContent.Text.Trim();//邮件内容
oJmailMessage.AddRecipient(txtAddress.Text.Trim(), "", "");//收件人邮箱地址
//发送附件
string FilePath = "";
int FileSize = 0;
if (upFile.PostedFile.FileName != "")
{
string attpath = upFile.PostedFile.FileName;
RondomName = Path.GetFileName(attpath);
FileSize = upFile.PostedFile.ContentLength; try
{
if (!Directory.Exists(Server.MapPath("~/UpLoad/WebMail") + "\\SendMail"))
{
Directory.CreateDirectory(Server.MapPath("~/UpLoad/WebMail") + "\\SendMail");
}
upFile.PostedFile.SaveAs(Server.MapPath("~/UpLoad/WebMail") + "\\SendMail\\" + attpath.Substring(attpath.LastIndexOf('\\') + 1, attpath.Length - (attpath.LastIndexOf('\\') + 1)));
FilePath = Server.MapPath("~/UpLoad/WebMail") + "\\SendMail\\" + attpath.Substring(attpath.LastIndexOf('\\') + 1, attpath.Length - (attpath.LastIndexOf('\\') + 1));
oJmailMessage.AddAttachment(@FilePath, false, FilePath.Substring(FilePath.LastIndexOf('.') + 1) + 3);//添加附件
}
catch (Exception ex)
{
throw (ex);
}
}
if (oJmailMessage.Send(smtpServer, false))
{
Response.Write("<script language=javascript>alert('邮件发送成功!');location = 'Default.aspx?MailFolderType=2'</script>"); }
else
{
Response.Write("<script>alert('发送失败,请检查邮件服务器的设置或未连上服务器!');</script>");
} oJmailMessage = null;
}
catch
{
Response.Write("<script>alert('发送失败,请检查邮件服务器的设置或未连上服务器!');</script>");
return;
}
}
}
}
}