添加附件到电子邮件 [英] Add Attachment To Email

查看:90
本文介绍了添加附件到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有通过Gmail发送电子邮件的C#代码非常好。我希望这个脚本也有发送附件的选项。

怎么做?

Hi,
I have C# code that send Email by Gmail very good. I want this script has option for sending attachment too.
How must do that?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace SendEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            Email();  
        }
        static void Email()
        {
            var fromAddress = new MailAddress("", "");
            var toAddress = new MailAddress("", "");
            const string fromPassword = "";
            const string subject = "";
            const string body = "";
            
           

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

            })
            {
               
                smtp.Send(message);
            }
        }
    }
}



问候,


Regards,

推荐答案

尝试:

Try:
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body,


})
{
    Attachment a = new Attachment("filename.txt");
    message.Attachments.Add(a);
    smtp.Send(message);
}



希望这有帮助。


Hope this helps.


这篇关于添加附件到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆