使用C#中的附件发送电子邮件 [英] Send email with Attachment in C#
问题描述
大家好我想发送一封附件的电子邮件(pdf,jpg)
这里是我写的。
Hi All i want to send an email with Attachment(pdf,jpg)
here what i have wrote.
public static string SalesNotifiesToProduction(string UserName, string company, string publication, int noticeid,
string projectanme, string summary,string SpecialInstructions,string logo,string PEmail,string Email,string Phonenumber,
string ResponseType,string EmailResponseTo,string Fax,string DirectURL,string PrintDates)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(PEmail);
mail.Bcc.Add("my email");
mail.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"].ToString());
mail.Subject = "OPBN Notice #: " + noticeid + " | Type Set Request";
//// attachment code here //
/// code ends //
string str = "";
if (ResponseType == "Yes")
{
string strFax = "";
if (Fax != null)
{
strFax=" , Fax: " + Fax;
}
string strEmail = "";
if (EmailResponseTo != null)
{
strEmail = " , Email: " + EmailResponseTo;
}
string strURL = "";
if (DirectURL != null)
{
strURL = " Contact URL: <a href="" + DirectURL + "">Click Here </a> ";
}
str =strURL + strEmail + strFax;
}
string Body = "Some text";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["mailserver"].ToString(), Convert.ToInt32(ConfigurationManager.AppSettings["port"]));
if (Convert.ToBoolean(ConfigurationManager.AppSettings["developement"]))
{
smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["username"].ToString(), ConfigurationManager.AppSettings["password"].ToString());
}
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
smtp.Send(mail);
return "Sent Notification Success!";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
this works perfectly fine and send email. But when i tried to attach a file using this code
String filename = VirtualPathUtility.GetFileName(logo);
if (filename != null && filename.Length > 0 )
{
//Attachment file = new Attachment(filename);
//mail.Attachments.Add(file);
var attachment = new Attachment(filename);
mail.Attachments.Add(attachment);
}
电子邮件不会消失。这里的logo是一个字符串,其中包含存储我的pdf / jpg文件的文件的URL /路径。所以我首先获取文件名,然后检查它是否为null,并发送带有Attachments.Add函数的电子邮件。现在我的文件存储在主项目目录/ PDF文件/文件夹中。
我必须先打开文件然后附上文件或文件名会找到文件在工作目录中并自动附加文件?
如果我在代码中缺少任何内容,请告诉我。
谢谢。
emails doesnot go out. here logo is a string with a URL/path of the file where my pdf/jpg file is stored. so i am getting the filename first and then checking if its null or not and send the email with Attachments.Add function. Now my file is stored in "Main project directory"/PDFfiles/ folder.
Do i have to open the file first then attache it or the file name will find the file in working directory and attache the file automatically?
Also let me know if i am missing anything in code.
Thanks.
推荐答案
使用以下代码添加附件,
附件附件= new Attachment(attachmentFilename,MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName (attachmentFilename);
disposition.Size = new FileInfo(attachmentFile name。.Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
mail.Attachments.Add(attachment);
Use following piece of code to add attachment,
Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName(attachmentFilename);
disposition.Size = new FileInfo(attachmentFilename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
mail.Attachments.Add(attachment);
Look在这个提示。它基本上涵盖了发送电子邮件的各个方面。
使用或不使用附件在C#中发送电子邮件:通用例程。 [ ^ ]
Look at this tip. It basically covers every aspect of sending email.
Sending an Email in C# with or without attachments: generic routine.[^]
这篇关于使用C#中的附件发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!