C# 使用Amazon AWS SMTP发送带有附件的电子邮件
我正在尝试使用amazon WS-SMTP发送电子邮件,发送成功,但我从未收到电子邮件。当我在没有附件的情况下发送它时,我成功地收到了它 这是我的密码C# 使用Amazon AWS SMTP发送带有附件的电子邮件,c#,amazon-web-services,amazon-ses,C#,Amazon Web Services,Amazon Ses,我正在尝试使用amazon WS-SMTP发送电子邮件,发送成功,但我从未收到电子邮件。当我在没有附件的情况下发送它时,我成功地收到了它 这是我的密码 // Create an SMTP client with the specified host name and port. using (SmtpClient client = new SmtpClient(ssHost, ssPort)) { // Create a network c
// Create an SMTP client with the specified host name and port.
using (SmtpClient client = new SmtpClient(ssHost, ssPort))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(ssSMTP_Username, ssSMTP_Password);
// Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
// the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;
// Send the email.
MailMessage message = new MailMessage();
try
{
//creates a messages with the all data
message.From = new MailAddress(ssFrom, ssFromName);
//message.To.Add(ssTo);
//To
List<String> toAddresses = ssTo.Split(',').ToList();
foreach (String toAddress in toAddresses)
{
message.To.Add(new MailAddress(toAddress));
}
//cc
List<String> ccAddresses = ssCC.Split(',').Where(y => y != "").ToList();
foreach (String ccAddress in ccAddresses)
{
message.CC.Add(new MailAddress(ccAddress));
}
//bcc
List<String> BccAddresses = ssBcc.Split(',').Where(y => y != "").ToList();
foreach (String ccAddress in ccAddresses)
{
message.Bcc.Add(new MailAddress(ccAddress));
}
//replyTo
if (ssReplyTo != null)
{
message.ReplyToList.Add(new MailAddress(ssReplyTo));
}
//email
message.Subject = ssSubject;
message.Body = ssBody;
message.IsBodyHtml = true;
//Attachment
foreach (RCAttachmentRecord attchmentRec in ssAttachmenstList){
System.IO.MemoryStream ms = new System.IO.MemoryStream(attchmentRec.ssSTAttachment.ssBinary);
Attachment attach = new Attachment(ms, attchmentRec.ssSTAttachment.ssFilename);
message.Attachments.Add(attach);
ssErrorMessage = ssErrorMessage + "||" + attchmentRec.ssSTAttachment.ssFilename+"lenght:"+attchmentRec.ssSTAttachment.ssBinary.Length;
}
client.Send(message);
}
//使用指定的主机名和端口创建SMTP客户端。
使用(SmtpClient客户端=新SmtpClient(ssHost、ssPort))
{
//使用SMTP用户名和密码创建网络凭据。
client.Credentials=new System.Net.NetworkCredential(ssSMTP\u用户名、ssSMTP\u密码);
//访问Amazon SES时使用SSL。SMTP会话将在未加密的连接上开始,然后
//客户端将发出STARTTLS命令以升级到使用SSL的加密连接。
client.enablesl=true;
//发送电子邮件。
MailMessage=新的MailMessage();
尝试
{
//创建包含所有数据的消息
message.From=新邮件地址(ssFrom,ssFromName);
//message.To.Add(ssTo);
//到
List toaddress=ssTo.Split(',').ToList();
foreach(toAddress中的字符串toAddress)
{
message.To.Add(新邮件地址(toAddress));
}
//抄送
列出ccaddress=ssCC.Split(',')。其中(y=>y!=“”)。ToList();
foreach(ccAddress中的字符串ccAddress)
{
message.CC.Add(新邮件地址(ccAddress));
}
//密件抄送
列出bccadirements=ssBcc.Split(',')。其中(y=>y!=“”)。ToList();
foreach(ccAddress中的字符串ccAddress)
{
message.Bcc.Add(新邮件地址(ccAddress));
}
//答复
if(ssReplyTo!=null)
{
message.ReplyToList.Add(新邮件地址(ssReplyTo));
}
//电子邮件
message.Subject=ssSubject;
message.Body=ssBody;
message.IsBodyHtml=true;
//附件:
foreach(SSAttachmentList中的RCAttachmentRecord附件中心){
System.IO.MemoryStream ms=新的System.IO.MemoryStream(attchCenter.ssStatAttachment.ssBinary);
Attachment attach=新附件(ms,attchcentre.ssSTAttachment.ssFilename);
message.Attachments.Add(附加);
ssErrorMessage=ssErrorMessage+“| |”+attchcentre.ssSTAttachment.ssFilename+“Length:”+attchcentre.ssSTAttachment.ssBinary.Length;
}
客户端。发送(消息);
}
来源:我认为你的问题可能是没有正确地将附件添加到邮件中 我成功地发送了一封带有附件的邮件。我从直接从中获取的代码开始。然后从中添加了代码 附件是“我的文档”文件夹中的Word文档
Lebowski.docx
。我建议您创建一个类似的简单Word文档,并运行以下代码以验证您可以在一封电子邮件中发送简单附件
以下代码成功地为我工作:
namespace SESTest
{
using System;
using System.Net.Mail;
namespace AmazonSESSample
{
class Program
{
static void Main(string[] args)
{
const String FROM = "from-email"; // Replace with your "From" address. This address must be verified.
const String TO = "to-email"; // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)";
const String BODY = "This email and attachment was sent through the Amazon SES SMTP interface by using C#.";
// Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
const String SMTP_USERNAME = "user-creds"; // Replace with your SMTP username.
const String SMTP_PASSWORD = "password"; // Replace with your SMTP password.
// Amazon SES SMTP host name.
const String HOST = "your-region.amazonaws.com";
// The port you will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
// STARTTLS to encrypt the connection.
const int PORT = 2587;
// Create an SMTP client with the specified host name and port.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
// Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
// the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;
// Send the email.
try
{
Console.WriteLine("Attempting to send an email through the Amazon SES SMTP interface...");
var mailMessage = new MailMessage()
{
Body = BODY,
Subject = SUBJECT,
From = new MailAddress(FROM)
};
mailMessage.To.Add(new MailAddress(TO));
//REMOVE THIS CODE
//System.IO.MemoryStream ms = new System.IO.MemoryStream(attchmentRec.ssSTAttachment.ssBinary);
//Attachment attach = new Attachment(ms, attchmentRec.ssSTAttachment.ssFilename);
//mailMessage.Attachments.Add(attach);
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
contentType.Name = "Lebowski.docx";
mailMessage.Attachments.Add(new Attachment("C:\\users\\luis\\Documents\\Lebowski.docx", contentType));
client.Send(mailMessage);
Console.WriteLine("Email sent!");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
}
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
}
}
一旦你证明你可以从你的帐户发送一封带有一个附件的电子邮件,那么你就可以开始研究如何将
ssAttachmentsList
中的二进制文件放入你的电子邮件中,并将正确的内容类型分配给每封邮件。但是你没有提供足够的代码或上下文,我现在无法确定。我希望这段代码我会帮你解决附件问题。你能发布更多代码吗?为了帮助你,我们需要知道你如何定义和填充SSAttachmentList
-谢谢。检查这个问题,也许能帮你解决吗?@Taterhead将该变量作为ArrayList@KaranShah谢谢,但没用。我想我找到问题了。我正在附加ZIP文件,但不知怎的,当我附加ZIP时,邮件被丢弃,如果我将文件重命名为ZI或发送图像,我总是会收到电子邮件。确定-ZIP附件-这很好。请记住,许多邮件安全筛选器会剥离并替换.ZIP附件。如果发送存储在值得信任的云位置(可能是s3 bucket)供他们下载。祝你好运。