任何人都可以整理以下代码,以便为电子邮件添加附件。
由于
<%@ 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>
答案 0 :(得分:3)
我对
怀疑new MailAttachment("myimage.jpg")
我怀疑你可能想要获得完整的路径,例如。
new MailAttachment(Server.MapPath("Myimage.jpg"))
答案 1 :(得分:3)
文件路径必须是完整路径,但除此之外,System.Web.Mail
已弃用/已废弃。您应该使用System.Net.Mail
API,有关示例,请参阅here。