这不应该那么复杂。
我只是想发送一封带有 .pdf 附件的电子邮件。电子邮件与附件一起发送和接收,但长度始终小于 1k。当我尝试在 Outlook 中打开它时,Adobe Reader 提示我无法打开它,因为它要么是“不受支持的文件类型”,要么是“文件已损坏(解码错误)”。这是代码:
function Send() {
var attachment = new Attachment(
input.Data.ReceiptFile.InputStream, "Receipt.pdf", "application/octet- stream"/*MediaTypeNames.Application.Pdf*/);
this.SendSubmitReceiptNotification(new Attachment[] { attachment });
}
private void SendSubmitReceiptNotification(Attachment[] attachments = null)
{
try
{
var submitForSamplesFromEmail = Config.GetSetting("SubmitForSamples:FromEmail");
var submitForSamplesToEmails = Config.GetSetting("SubmitForSamples:ToEmails");
if (string.IsNullOrWhiteSpace(submitForSamplesFromEmail) || string.IsNullOrWhiteSpace(submitForSamplesToEmails))
{
// From/To missing.
return;
}
var toEmails = submitForSamplesToEmails.Split(
new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var to = toEmails.ToArray();
var from = submitForSamplesFromEmail;
var subject = "Test";
var body = "<strong>Hi There</strong>";
var isHtml = true;
//
// make the email object
MailMessage message = new MailMessage();
// set whether or not it's an html message
message.IsBodyHtml = isHtml;
// add the from address
message.From = new MailAddress(from);
// add the subject
message.Subject = subject;
// add the message body
message.Body = body;
// add the attachments
if (attachments != null && attachments.Length > 0)
{
// there are attachments that need attaching
foreach (Attachment att in attachments)
{
// add each attachemnt in order
message.Attachments.Add(att);
}
}
// add the list of who to send the email to
foreach (string address in to)
{
message.To.Add(address);
}
//
//message.HeadersEncoding = System.Text.Encoding.Unicode
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(message);
//Email.Send(to, from, subject, body, isHtml, attachments);
}
catch (Exception e)
{
var error = e.Message;
}
}