如何将文件附加到来自网址的电子邮件中 [英] How can I attach a file to an email from a url
本文介绍了如何将文件附加到来自网址的电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个上传到服务器上的图片网址(http://xyz.com/photos/pic.png),我正在运行时获取该图片路径。
我想要发送带附件的邮件(带有该图像文件),我有一个接受文件路径作为字符串参数的服务以及其他所需信息(如TO,CC,...)。
如果我将URL(http://xyz.com/photos/pic.png)作为服务的文件路径传递,我会收到错误。
I have a image URL("http://xyz.com/photos/pic.png") that is uploaded on server and I am getting that image path at run time.
I want to sent mail with attachment(with that image file), I have a service that accept file path as string parameter along with other required information(like TO,CC,...).
I am getting error if I am passing URL("http://xyz.com/photos/pic.png") as file path to service.
推荐答案
您可以将图像保存在服务器上,然后将图像路径传递给您的电子邮件服务。
You can save the image on your server and then pass the image path to your Email Service.
public static void SaveImageFromUrl(string imageUrl)
{
//Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(imageUrl);
// Set the Method property of the request to POST.
request.Method = "GET";
WebResponse response = null;
// Get the response.
try
{
response = request.GetResponse();
}
catch (Exception Ex)
{
//error
}
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
//path to save image
string ImagePath = HttpContext.Current.Server.MapPath("~/attachments/") + Guid.NewGuid() + ".jpg";//check your extension
//Save the image in your server
using (Stream s = File.Create(ImagePath))
{
dataStream.CopyTo(s);
}
//Now pass the ImagePath in your Email service
}
这篇关于如何将文件附加到来自网址的电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文