使用 C# 添加附件到电子邮件

发布于 2024-10-18 07:43:04 字数 1090 浏览 4 评论 0原文

我使用此答案中的以下代码通过 Gmail 在 .NET 中发送电子邮件。我遇到的问题是在电子邮件中添加附件。如何使用下面的代码添加附件?

using System.Net.Mail;

var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body
    })
{
    smtp.Send(message);
}

I'm using the following code from this answer Sending email in .NET through Gmail. The trouble I'm having is adding an attachment to the email. How would I add an attachment using the code below?

using System.Net.Mail;

var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body
    })
{
    smtp.Send(message);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尝蛊 2024-10-25 07:43:04

通过 new MailMessage 方法调用创建的 message 对象具有属性 .Attachments

例如:

message.Attachments.Add(new Attachment(PathToAttachment));

The message object created from your new MailMessage method call has a property .Attachments.

For example:

message.Attachments.Add(new Attachment(PathToAttachment));
伏妖词 2024-10-25 07:43:04

使用 MSDN:

// Create  the file attachment for this e-mail message.
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);

Using the Attachment class as proposed in the MSDN:

// Create  the file attachment for this e-mail message.
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);
花想c 2024-10-25 07:43:04

像这样更正您的代码

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);

http://csharp.net-informations.com/ communications/csharp-email-attachment.htm

希望这会对您有所帮助。

瑞奇

Correct your code like this

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);

http://csharp.net-informations.com/communications/csharp-email-attachment.htm

hope this will help you.

ricky

双马尾 2024-10-25 07:43:04

提示:如果之后添加附件,邮件正文会被附件文件路径覆盖,因此先附加后添加正文

mail.Attachments.Add(new Attachment(file));
mail.Body = "body";

Hint: mail body is overwritten by attachment file path if attachment is added after, so attach first and add body later

mail.Attachments.Add(new Attachment(file));
mail.Body = "body";
画中仙 2024-10-25 07:43:04

一行答案:

mail.Attachments.Add(new System.Net.Mail.Attachment("pathToAttachment"));

A one line answer:

mail.Attachments.Add(new System.Net.Mail.Attachment("pathToAttachment"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文