不是SiteMap中的有效虚拟路径

时间:2013-04-26 12:08:25

标签: c# asp.net email-attachments

您好我想从Asp.net发送图片邮件。

我收到如下错误:“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);
    }

1 个答案:

答案 0 :(得分:2)

删除

Server.MapPath

来自

LinkedResource imagelink = new LinkedResource(Server.MapPath(ImagePath)+  @post, "image/png");
路径位于应用程序(虚拟路径)中时使用

Server.MapPath。您的图片网址是直接网址或物理路径,因此不需要MapPath

  

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

如果您的图像在VisualStudio解决方案中,则可以使用MapPath