C#:如何将 PDF 文件附加到电子邮件?
C#: How can I attach a PDF file to an eMail?
下面是我尝试向收件人发送电子邮件的代码。我想附加一个保存在项目文件夹中的 .PDF
文件。我尝试使用:
mail.Attachments.Add(new Attachment("c:\temp\example.txt"));
但它不起作用。如何附加文件?
C# 代码示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SendEmail
{
public partial class SendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SendMail();
}
protected void SendMail()
{
var fromAddress = "";
var toAddress = "";
//Password of your gmail address
const string fromPassword = "";
string subject = "INCOMPLETE APPLICATION CASE ID [CASE ID]";
string body = "Your Incomplete Grade Application has been Result[]";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "10.12.46.3";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
}
}
现在没有时间发表评论,但这会让你摆脱束缚。
private void SendIt()
{
var fromAddress = "";
var toAddress = "";
//Password of your gmail address
const string fromPassword = "";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
MailMessage msg = new MailMessage();
MailAddress ma = new MailAddress(fromAddress);
msg.To.Add(toAddress);
msg.From = ma;
msg.Attachments.Add(new Attachment(@"C:\temp\myreport.log"));
msg.Body = "Your body message";
msg.Subject = "Your subject line";
smtp.Send(msg);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace SendEmail
{
public partial class SendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SendMail();
}
protected void SendMail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("");
mail.To.Add("");
mail.Subject = "INCOMPLETE APPLICATION CASE ID [CASE ID]";
mail.Body = "Your Incomplete Grade Application has been Result[]";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(Server.MapPath("files/test.pdf"));
mail.Attachments.Add(attachment);
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "10.12.46.3";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("", "");
}
smtp.Send(mail);
}
}
}
我看到其他人还没有处理附件,请记住附件可以保持打开状态,所以如果您想重新发送它,您将无法重新发送,因为该文件将继续使用。
var 附件 = 新附件(文件路径,MediaTypeNames.Application.Pdf);
Message.Attachments.Add(附件);
//其他代码然后发送邮件...
attachment.Dispose();
下面是我尝试向收件人发送电子邮件的代码。我想附加一个保存在项目文件夹中的 .PDF
文件。我尝试使用:
mail.Attachments.Add(new Attachment("c:\temp\example.txt"));
但它不起作用。如何附加文件?
C# 代码示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SendEmail
{
public partial class SendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SendMail();
}
protected void SendMail()
{
var fromAddress = "";
var toAddress = "";
//Password of your gmail address
const string fromPassword = "";
string subject = "INCOMPLETE APPLICATION CASE ID [CASE ID]";
string body = "Your Incomplete Grade Application has been Result[]";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "10.12.46.3";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
}
}
现在没有时间发表评论,但这会让你摆脱束缚。
private void SendIt()
{
var fromAddress = "";
var toAddress = "";
//Password of your gmail address
const string fromPassword = "";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
MailMessage msg = new MailMessage();
MailAddress ma = new MailAddress(fromAddress);
msg.To.Add(toAddress);
msg.From = ma;
msg.Attachments.Add(new Attachment(@"C:\temp\myreport.log"));
msg.Body = "Your body message";
msg.Subject = "Your subject line";
smtp.Send(msg);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace SendEmail
{
public partial class SendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SendMail();
}
protected void SendMail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("");
mail.To.Add("");
mail.Subject = "INCOMPLETE APPLICATION CASE ID [CASE ID]";
mail.Body = "Your Incomplete Grade Application has been Result[]";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(Server.MapPath("files/test.pdf"));
mail.Attachments.Add(attachment);
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "10.12.46.3";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("", "");
}
smtp.Send(mail);
}
}
}
我看到其他人还没有处理附件,请记住附件可以保持打开状态,所以如果您想重新发送它,您将无法重新发送,因为该文件将继续使用。
var 附件 = 新附件(文件路径,MediaTypeNames.Application.Pdf);
Message.Attachments.Add(附件);
//其他代码然后发送邮件...
attachment.Dispose();