簡體   English   中英

C#如何從遠程網站發送帶有附件的電子郵件

[英]C# How to send email with attachment from a remote website

(必須重新發布,因為我在原始內容中做了一些非常愚蠢的事情...)

注意:我是編程新手。

我可以從計算機通過電子郵件發送附件,但是如何通過互連網發送附件,例如: http ://blah.com/image.jpg而不是@“ C:\\ Users \\ me \\ pictures \\ picture。 jpg”?

提前致謝。

這是我所擁有的:

class Program
    {
        public const string GMAIL_SERVER = "smtp.gmail.com";
        public const int PORT = 587;

        static void Main(string[] args)
        {
            Console.WriteLine("Mail To:");
            MailAddress to = new MailAddress(Console.ReadLine());

            Console.WriteLine("Mail From:");
            MailAddress from = new MailAddress(Console.ReadLine());

            MailMessage mail = new MailMessage(from, to);

            Console.WriteLine("Subject:");
            mail.Subject = Console.ReadLine();

            mail.Attachments.Add(new Attachment(@"C:\Users\me\pictures\picture.jpg"));
            //Not sure how to send from a remote website...

            Console.WriteLine("Your Message:");
            mail.Body = Console.ReadLine();

            SmtpClient smtp = new SmtpClient(GMAIL_SERVER, PORT);
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;

            smtp.Credentials = new NetworkCredential(
                "myemail", "password");
            smtp.EnableSsl = true;
            Console.WriteLine("Sending email... Please wait...");

            smtp.Send(mail);
            Console.WriteLine("Finshed!\n");

        }
    }

您已經需要下載文件並將其添加為附件。

有點像

string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
    client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}

將下載文件。

首先下載映像,然后僅使用本地文件,或者使用流並直接在附件的構造函數中使用它。 請參閱https://msdn.microsoft.com/library/system.net.mail.attachment(v=vs.110).aspx 對於第一種方法,只需將WebClient與.DownloadFile一起使用。 對於第二個,請嘗試:

WebClient client = new WebClient();
client.OpenReadCompleted += (s, e) =>
     {
         // e.Result is your stream containing the image. 
     };
client.OpenReadAsync(new Uri(imageUrl));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM