1

我正在处理一个必须发送带有附件的电子邮件的项目。我已经实现了发送邮件,但是我不知道如何附加一个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);
}
4

3 回答 3

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);
于 2013-07-07T08:11:35.133 回答
0

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

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

pathToExcelFile您要发送的文件的文件路径在哪里。

确保您有权访问该文件。

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

于 2013-07-07T08:11:00.990 回答
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);
        }
于 2013-07-07T08:14:28.083 回答