C# 如何使用PDF文件发送邮件
我想用C#发送一个带有邮件的pdf文件。我知道如何发送邮件,但我不知道如何发送带有pdf文件的邮件:( 例如,我在文件夹C:\test.pdf中有一个pdf文件 这是我的密码:C# 如何使用PDF文件发送邮件,c#,asp.net,email,attachment,C#,Asp.net,Email,Attachment,我想用C#发送一个带有邮件的pdf文件。我知道如何发送邮件,但我不知道如何发送带有pdf文件的邮件:( 例如,我在文件夹C:\test.pdf中有一个pdf文件 这是我的密码: private void SendEmail(string pdfpath,string firstname, string lastname, string title, string company, string mailfrom,string mailto) { try { Ma
private void SendEmail(string pdfpath,string firstname, string lastname, string title, string company, string mailfrom,string mailto)
{
try
{
MailMessage m = new MailMessage();
System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
m.From = new System.Net.Mail.MailAddress(mailfrom);
m.To.Add(mailto);
m.Subject = "Company Gastzugang (" + lastname + ", " + firstname + ")";
// what I must do for sending a pdf with this email
m.Body = "Gastzugangdaten sind im Anhang enthalten";
sc.Host = SMTPSERVER; // here is the smt path
sc.Send(m);
}
catch (Exception ex)
{
error.Visible = true;
lblErrorMessage.Text = "Folgender Fehler ist aufgetreten: " + ex.Message;
}
}
您需要将其添加为附件 查看有关此的MSDN文档-您可以这样做:
var filename = @"c:\test.pdf";
m.Attachments.Add(new Attachment(filename));
这是我的全部代码:
public void SendMail()
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("contact@yourwebsite.com");
string s = txtEmail.Text;
msg.To.Add(txtEmail.Text);
msg.Body = "<html><body><img src='~/images/back.png'/><br></body></html>";
msg.IsBodyHtml = true;
msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
Attachment at = new Attachment(Server.MapPath("~/Main/images/English.pdf"));
//Dim at1 As New Attachment(Server.MapPath("~/Main/images/English.pdf"))
msg.Attachments.Add(at);
//msg.Attachments.Add(at1)
msg.Priority = MailPriority.High;
msg.Subject = "Special Gift";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("yourgmail@gmail.com", "gmailpassword");
smtp.Send(msg);
}
public void SendMail()
{
MailMessage msg=新的MailMessage();
msg.From=新邮件地址(“contact@yourwebsite.com");
字符串s=txtEmail.Text;
msg.To.Add(txtEmail.Text);
msg.Body=“
”;
msg.IsBodyHtml=true;
msg.BodyEncoding=System.Text.Encoding.GetEncoding(“utf-8”);
附件地址=新附件(Server.MapPath(“~/Main/images/English.pdf”);
//Dim at1作为新附件(Server.MapPath(“~/Main/images/English.pdf”))
msg.Attachments.Add(at);
//msg.Attachments.Add(at1)
msg.Priority=MailPriority.High;
msg.Subject=“特殊礼物”;
SmtpClient smtp=新SmtpClient();
smtp.Host=“smtp.gmail.com”;
smtp.EnableSsl=true;
smtp.Credentials=新系统.Net.NetworkCredential(“yourgmail@gmail.com“,“Gmail密码”);
smtp.Send(msg);
}
您需要它作为附件吗?
public void SendMail()
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("contact@yourwebsite.com");
string s = txtEmail.Text;
msg.To.Add(txtEmail.Text);
msg.Body = "<html><body><img src='~/images/back.png'/><br></body></html>";
msg.IsBodyHtml = true;
msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
Attachment at = new Attachment(Server.MapPath("~/Main/images/English.pdf"));
//Dim at1 As New Attachment(Server.MapPath("~/Main/images/English.pdf"))
msg.Attachments.Add(at);
//msg.Attachments.Add(at1)
msg.Priority = MailPriority.High;
msg.Subject = "Special Gift";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("yourgmail@gmail.com", "gmailpassword");
smtp.Send(msg);
}