如何将文件附加到电子邮件中?

时间:2013-07-07 07:56:59

标签: asp.net email methods email-attachments

我正在开发一个必须发送带附件的电子邮件的项目。我已经实现了发送邮件,但我不知道如何附加excel文件。

有人能指出我正确的方向吗?到目前为止,这是我的代码:

private void sendmail(string Tomailid, string name)
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("ehsimsemail@gmail.com");
    mail.To.Add(Tomailid);
    mail.Subject = "Test Mail";

    StringBuilder sb = new StringBuilder();

    sb.Append("Dear ");
    sb.Append(string.Format("{0} ", name));
    //sb.Append("Yuvaraj");
    sb.Append(Environment.NewLine);
    sb.Append(Environment.NewLine);
    sb.Append("UCR No : ");
    sb.Append("3256987");
    sb.Append(" ");
    sb.Append("HAS BEEN SENT FOR YOUR REVIEW AND APPROVAL");
    sb.Append(Environment.NewLine);
    sb.Append(Environment.NewLine);
    sb.Append("For action, Please click Here");
    sb.Append(Environment.NewLine);
    sb.Append("This is a automatic generated report from Advanced Incident Management System (EHS-IMS). Please do not reply to this mail.");
    sb.Append(Environment.NewLine);
    sb.Append(Environment.NewLine);
    sb.Append("Thanks & Regards,");
    sb.Append(Environment.NewLine);
    sb.Append("EHSIMS TEAM");      


    mail.Body = sb.ToString();
    SmtpClient SmtpServer = new SmtpClient();
    SmtpServer.Host = "smtp.gmail.com";
    SmtpServer.Port = 587;

    SmtpServer.EnableSsl = true;
    SmtpServer.Credentials = new System.Net.NetworkCredential("ehsimsemail@gmail.com", "ehsims123");

    SmtpServer.Send(mail);
}

3 个答案:

答案 0 :(得分:1)

这很容易。互联网上有这么多资料。

看看这个链接: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

短代码:

Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);

答案 1 :(得分:0)

您必须将文件附加到您的邮件中。

mail.Attachments.Add(New Attachment(pathToExcelFile))

其中pathToExcelFile是您要发送的文件的文件路径。

确保您拥有该文件的访问权限。

有关详细信息,请参阅以下内容: http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx

答案 2 :(得分:0)

 protected void btnSendMail_Click(object sender, EventArgs e)
        {
            string to = "reciever@gmail.com";
            string from = "frommail@gmail.com";
            string subject = "Mail Subject";
            string body = "Mail Message";
            MailMessage msgObj = new MailMessage();
            msgObj.To.Add(to);
            msgObj.Subject = subject;
            msgObj.Body = body;
            msgObj.IsBodyHtml = true;
            msgObj.Attachments.Add(new Attachment(@"D:\sample.xlx"));
            MailAddress objMail = new MailAddress(from, "emailCampaign", System.Text.Encoding.UTF8);
            msgObj.From = objMail;
            SmtpClient smtp = new SmtpClient();
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.Credentials = new System.Net.NetworkCredential(from, "password");
            smtp.EnableSsl = true;
            smtp.Send(msgObj);
        }