2

我想用 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
    {
        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;
    }
}
4

4 回答 4

4

你可以这样做:

var filename = @"c:\test.pdf";
m.Attachments.Add(new Attachment(filename));
于 2013-02-07T07:33:12.037 回答
2

您需要将其添加为附件。

检查有关此的 MSDN 文档 - http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

于 2013-02-07T07:30:02.160 回答
1
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("filename");
m.Attachments.Add(attachment);
于 2013-02-07T07:34:53.570 回答
0

这是我的整个代码:

    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);
}
于 2013-02-07T07:38:17.180 回答