但它说“SMTP 服务器需要安全连接,或者客户端未经过身份验证。服务器响应是:5.7.0 需要身份验证。了解更多信息”
或“发送邮件失败。”
我的代码如下:
protected void BtnSendEmail_Click(对象发件人, EventArgs e) { Send_Email("[email protected]","这是一封带有附件的邮件!","你好,祝你有美好的一天。我已经给你发送了一封带有附件的电子邮件!"); }
protected string Bind_Invoice()
{
string op = null;
try
{
con = new SqlConnection(CommonClass.Get_DB_Path);
con.Open();
cmd = new SqlCommand("BIND_PURCHASE_INVOICE", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 10000;
cmd.Parameters.AddWithValue("@USER", Session["username"].ToString());
SqlParameter RuturnValue = new SqlParameter("@RETVAL", SqlDbType.VarChar, 18000);
RuturnValue.Direction = ParameterDirection.Output;
cmd.Parameters.Add(RuturnValue);
cmd.ExecuteNonQuery();
op = cmd.Parameters["@RETVAL"].Value.ToString();
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
if (ex.Message != null)
{
op = ex.Message;
con.Close();
}
}
return op;
}
protected void Send_Email(string ToAddress, string MailSubject, string MailBody)
{
try
{
MailMessage message = new MailMessage();
message.To.Add(ToAddress);// Email-ID of Receiver
message.Subject = MailSubject;// Subject of Email
message.From = new System.Net.Mail.MailAddress("MYUSERNAME");// Email-ID of Sender
message.IsBodyHtml = true;
string PDFdata = Bind_Invoice();
MemoryStream file = new MemoryStream(PDFGenerate(PDFdata, Server.MapPath("img/bg.jpg")).ToArray());
file.Seek(0, SeekOrigin.Begin);
Attachment data = new Attachment(file, "RunTime_Attachment.pdf", "application/pdf");
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.DateTime.Now;
disposition.ModificationDate = System.DateTime.Now;
disposition.DispositionType = DispositionTypeNames.Attachment;
message.Attachments.Add(data);//Attach the file
message.Body = MailBody;
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Host = "MY HOST ADDRESS";//name or IP-Address of Host used for SMTP transactions
SmtpMail.Port = 465;//Port for sending the mail
SmtpMail.Credentials = new System.Net.NetworkCredential("MYUSERNAME", "MYPASS");//username/password of network, if apply
SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpMail.EnableSsl = true;
SmtpMail.UseDefaultCredentials = false;
SmtpMail.ServicePoint.MaxIdleTime = 0;
SmtpMail.ServicePoint.SetTcpKeepAlive(true, 2000, 2000);
message.BodyEncoding = Encoding.Default;
message.Priority = MailPriority.High;
SmtpMail.Send(message); //Smtpclient to send the mail message
Response.Write("Email has been sent !");
}
catch (Exception ex)
{
Response.Write("Failed !");
}
}
private MemoryStream PDFGenerate(string message, string ImagePath)
{
MemoryStream output = new MemoryStream();
Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, output);
pdfDoc.Open();
Paragraph Text = new Paragraph(message);
pdfDoc.Add(Text);
byte[] file;
file = System.IO.File.ReadAllBytes(ImagePath);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(file);
jpg.ScaleToFit(550F, 200F);
pdfDoc.Add(jpg);
pdfWriter.CloseStream = false;
//pdfDoc.Close();
output.Position = 0;
return output;
}
任何帮助将不胜感激......
如果您遇到身份验证失败,这应该是与附件不同的问题。许多提供商(尤其是 Google)现在需要双因素身份验证,如果您使用这些提供商之一来发送电子邮件,则可能必须首先解决这个问题。对于 Gmail,您基本上必须获取应用程序登录时使用的密码,而不是普通密码。这神奇地满足了两因素要求并允许您发送电子邮件。以下是 Google 支持论坛的相关讨论: