[英]C# How to send email with attachment from a remote website
(Had to repost this because I did something pretty dumb in the original...) (必须重新发布,因为我在原始内容中做了一些非常愚蠢的事情...)
NOTE: I'm very new at programming. 注意:我是编程新手。
I can email an attachment from my computer, but how do I send an attachment via the interwebs, for example: http://blah.com/image.jpg as opposed to @"C:\\Users\\me\\pictures\\picture.jpg"? 我可以从计算机通过电子邮件发送附件,但是如何通过互连网发送附件,例如: http ://blah.com/image.jpg而不是@“ C:\\ Users \\ me \\ pictures \\ picture。 jpg”?
Thanks in advance. 提前致谢。
Here's what I have: 这是我所拥有的:
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");
}
}
You would need to download the file and add it as an attachment as you have already done. 您已经需要下载文件并将其添加为附件。
Somthing like 有点像
string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}
will download the file. 将下载文件。
Download the Image first and then just use the local file, or use a Stream and directly use it in the Attachment's Constructor. 首先下载映像,然后仅使用本地文件,或者使用流并直接在附件的构造函数中使用它。 See https://msdn.microsoft.com/library/system.net.mail.attachment(v=vs.110).aspx .
请参阅https://msdn.microsoft.com/library/system.net.mail.attachment(v=vs.110).aspx 。 For the first approach, just use a WebClient with .DownloadFile.
对于第一种方法,只需将WebClient与.DownloadFile一起使用。 For the second, try:
对于第二个,请尝试:
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.