如何将html电子邮件内容作为任何格式的附件发送。
我正在使用传真API,它允许我发送带有相应传真号码的附件。 API将传真附件提供给传真号码。
这是我的代码
public string SendFax(int ID)
{
MailMessage message = new MailMessage();
//To address
message.To.Add(new MailAddress("email@domain.com"));
message.Subject = "My Subject";
//Specify true if it is html message
message.IsBodyHtml = true;
Class Details = new Class();
Details = Details.GetDetails(ID);
string mailBody = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath("~/Documents/EmailTemplates/FaxTemplate.htm"));
//Prescriber details.
mailBody = mailBody.Replace("%FirstName%", Details.FirstName);
mailBody = mailBody.Replace("%LastName%", Details.LastName);
mailBody = mailBody.Replace("%OfficeName%", Details.PracticeName);
mailBody = mailBody.Replace("%AddressLIne1%", Details.AddressLine1);
mailBody = mailBody.Replace("%Phone%", Details.Phone);
mailBody = mailBody.Replace("%FaxNumber%", Details.Fax);
mailBody = mailBody.Replace("%DEANumber%", Details.DEANUmber);
message.Body = mailBody;
status = Send(message);
if (status.Equals(string.Empty))
status = "Failure sending Fax" + "|" + "0";
else
status = "Successfully Faxed";
return status;
}
public string Send(MailMessage message)
{
string host = WebConfigurationManager.AppSettings["SMTPHost"].ToString();
int port = Convert.ToInt32(WebConfigurationManager.AppSettings["SMTPPort"]);
string username = WebConfigurationManager.AppSettings["SMTPUsername"].ToString();
string password = WebConfigurationManager.AppSettings["SMTPPwd"].ToString();
string fromAddress = WebConfigurationManager.AppSettings["EmailFrom"].ToString();
SmtpClient smtpC = new SmtpClient();
smtpC.Host = host;
smtpC.Port = port;
//smtpC.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpC.Credentials = new System.Net.NetworkCredential(username, password);
smtpC.EnableSsl = false;
message.From = new MailAddress(fromAddress);
smtpC.Send(message);
status = "Success";
return status;
}
答案 0 :(得分:1)
尝试这样的事情
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add(txtTo.Text);
mail.From = new MailAddress(txtFrom.Text);
mail.Subject = txtSubject.Text;
mail.Body = txtMessage.Text;
mail.IsBodyHtml = true;
//Attach file using FileUpload Control and put the file in memory stream
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("email@domain.com", "YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
如果要将数据流添加到MailMessage,请尝试使用
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write("Hello its my sample file");
writer.Flush();
writer.Dispose();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = "myFile.txt";
// I guess you know how to send email with an attachment
// after sending email
ms.Close();