添加附件到电子邮件

时间:2010-09-24 10:15:22

标签: asp.net vb.net

任何人都可以整理以下代码,以便为电子邮件添加附件。

由于

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="ISO-8859-1" Debug="true" %>
<% @Import Namespace="System.Web.Mail" %>
<% @Import Namespace="IO" %>

<script language="vb" runat="server">

Sub btnSendEmail_Click(sender as Object, e as EventArgs)

  Dim objMM as New MailMessage()

  objMM.To = "my@email.co.uk"
  objMM.From = "their@email.co.uk"
  objMM.BodyFormat = MailFormat.HTML
  objMM.Priority = MailPriority.Normal
  objMM.Subject = "Attachment test"
  objMM.Body = "There should be an attachment with this email"

  objMM.Attachments.Add(new MailAttachment("myimage.jpg"))

  SmtpMail.SmtpServer = "localhost"
  SmtpMail.Send(objMM)

End Sub

</script>

<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:Button runat="server" id="btnSendEmail" Text="Send email" OnClick="btnSendEmail_Click" />
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

我对

怀疑
new MailAttachment("myimage.jpg")

我怀疑你可能想要获得完整的路径,例如。

new MailAttachment(Server.MapPath("Myimage.jpg"))

答案 1 :(得分:3)

文件路径必须是完整路径,但除此之外,System.Web.Mail已弃用/已废弃。您应该使用System.Net.Mail API,有关示例,请参阅here