2013-04-26 72 views
0

您好,我想从Asp.net发送图像邮件。不是SiteMap中的有效虚拟路径

我有这样的错误::'http://domain-name.com/slideshow/original/Jellyfish.png'不是一个有效的虚拟路径。

我的代码是:

try 
    { 
     string _SenderEmailID = ReciverEmailID.Text.ToString(); 
     string _ReciverEmailID = ReciverEmailID.Text.ToString(); 
     string _Sender = FullName.Text.ToString(); 
     string post = "JellyBeans.png"; 
     string ImagePath = "http://www.domain-name.com/slideshow/original/"; 
     string iImage = ImagePath + post; 
     img1.ImageUrl = ImagePath; 

     MailMessage mail = new MailMessage(); 

     mail.To.Add(_ReciverEmailID); 
     mail.From = new MailAddress(_SenderEmailID); 

     mail.Subject = _Sender + " sent you a mail from 'www.domain-name.com"; 
     string Body = "<img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >"; 
     AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html"); 
     LinkedResource imagelink = new LinkedResource(Server.MapPath(ImagePath)+ @post, "image/png"); 
     imagelink.ContentId = "imageId"; 
     imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 
     htmlView.LinkedResources.Add(imagelink); 
     mail.AlternateViews.Add(htmlView); 
     SmtpClient smtp = new SmtpClient(); 
     smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; 
     smtp.Send(mail); 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 

回答

2

从线

LinkedResource imagelink = new LinkedResource(Server.MapPath(ImagePath)+ @post, "image/png"); 

Server.MapPath删除

Server.MapPath 

当路径是在您的应用程序,虚拟路径被使用。您的图片网址是直接网址或物理路径,因此不需要MapPath

返回与Web服务器上指定的 虚拟路径对应的物理文件路径。

如果您的图像位于您的VisualStudio解决方案中,您将使用MapPath

+0

当我从LinkedResource中删除Server.MapPath imagelink = new LinkedResource(iImage,“image/png”); 现在我面临新的错误:URI格式不支持,而。 @Sam Leach – 2013-04-26 12:35:38

+0

你可以把图像放到你的网络服务器上吗? – 2013-04-26 12:43:07

+0

是的,它已经是他们的@Sam Leach – 2013-04-26 12:56:37