62,041
社区成员
发帖
与我相关
我的任务
分享
public void sendEmail(string sender, string senderuser, string euser, string epwd, string receiver, string subject, string body, string eserver)
{
jmail.MessageClass jmMessage = new jmail.MessageClass();
jmMessage.Charset = "GB2312";
jmMessage.ISOEncodeHeaders = false;
jmMessage.From = sender;
jmMessage.FromName = senderuser;
jmMessage.Subject = subject;
jmMessage.MailServerUserName = euser;
jmMessage.MailServerPassWord = epwd;
jmMessage.AddRecipient(receiver, "", "");
// if (this.upFile.PostedFile.ContentLength != 0)
//{
// string sFilePath = this.upFile.PostedFile.FileName;
// jmMessage.AddAttachment(@sFilePath, true, "");
// }
jmMessage.Body = body;
if (jmMessage.Send(eserver, false))
{
// Response.Write("<script language=javascript>alert('发送成功')</script>");
}
else
{
Response.Write("<script language=javascript>alert('发送失败,请仔细检查邮件服务器的设置是否正确!')</script>");
jmMessage = null;
return;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;
namespace MailSender
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// 创建一个附件对象
objMailAttachment = new MailAttachment("C:\\1.xml");//发送邮件的附件
// 创建邮件消息
objMailMessage = new MailMessage();
objMailMessage.From = "mytest110@sina.com";//源邮件地址
objMailMessage.To = "********@qq.com";//目的邮件地址
objMailMessage.Subject = "邮件发送标题:你好";//发送邮件的标题
objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容
objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中
//接着利用sina的SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "mytest110");
//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "******");
//如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为:530 Authentication required
//SMTP地址
SmtpMail.SmtpServer = "smtp.sina.com";
// 开始发送邮件
// 在发送之前,去新浪邮箱里开启POP/SMTP设置 邮箱设置->账户->POP/SMTP设置->开启
// 否则会报错误0x80040217. The server response was not available
SmtpMail.Send(objMailMessage);
}
}
}
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSendEmail_Click(object sender, EventArgs e)
{
string _x_ccono = X_CCONo.Text.Trim();
if (!_x_ccono.Equals(string.Empty))
{
XJY_YS.BLL.CF.C_Complaints complaintsBll = new XJY_YS.BLL.CF.C_Complaints();
XJY_YS.Model.CF.C_Complaints complaintsModel = complaintsBll.GetModel(_x_ccono);
if (complaintsModel != null)
{
CreateExcel(complaintsModel);//创建Excel
GC.Collect(); //Kill进程
//主要是这段邮件内容调用
try
{
string from = FromName;
string password = FromPass;
string to = ToName;
//取出用户名
int nameLength = from.IndexOf('@');
string username = from.Substring(0, nameLength);
string subject = "";//主题
string body = "";//内容
string server = "smtp.gmail.com";//服务器地址 SMTP (这里是谷歌邮箱的)
string fileEndName = "文件.xls";
string fileUpLoad = Server.MapPath("~/EmailSend/" + fileEndName);
SendEmail(username, password, server, from, to, subject, body, fileUpLoad);
}
catch
{
//
}
}
}
}
/// <summary>
/// 发送邮件方法
/// </summary>
/// <param name="username">发送邮件方的登陆用户名(注:这里并不是邮箱名称,而是邮件@前的名称)</param>
/// <param name="pwd">登陆密码</param>
/// <param name="server">SMTP传输协议服务器地址(发送邮件方的服务器地址)</param>
/// <param name="fromemail">发送邮件方的邮箱账号</param>
/// <param name="tomail">接收邮件方的邮箱账号</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文内容</param>
/// <param name="fileUpLoad">邮件附件(上传到服务器的Excel文件)</param>
/// <returns>成功返回 True / 失败返回 False</returns>
private bool SendEmail(string username, string pwd, string server, string fromemail, string tomail, string subject, string body, string fileUpLoad)
{
bool isFlag = true;
try
{
//创建传输协议smtp对象
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
//创建发送邮件类对象
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//发送邮件方
mail.From = new System.Net.Mail.MailAddress(fromemail);
//接收邮件方
mail.To.Add(new System.Net.Mail.MailAddress(tomail));
//附件文件
mail.Attachments.Add(new Attachment(fileUpLoad));
//邮件主题
mail.Subject = subject;
//正文内容
mail.Body = body;
//是否Html格式
mail.IsBodyHtml = true;
//SMTP服务器地址
smtp.Host = server;
//发送连接时间
smtp.Timeout = int.MaxValue;
smtp.UseDefaultCredentials = false;
//发送端口
smtp.Port = 587;
//是否加密
smtp.EnableSsl = true;
//发送邮件方的身份验证(登陆名称/密码)
smtp.Credentials = new System.Net.NetworkCredential(username, pwd);
//发送邮件对象
smtp.Send(mail);
}
catch
{
isFlag = false;
}
return isFlag;
}