将函数从asp转换为asp.net发送邮件

时间:2014-01-17 06:39:00

标签: asp.net vb.net asp-classic

我有一个函数,它将创建的电子邮件发送到经典的asp并希望转换asp.net Vb中的函数。首先是我的ASP功能,在我之后尝试转换为vb。我做错了什么以及如何从我的webConfig中读取mailSettings。

 Function MailIt(subject, from, email, body)

   Dim mail As New MailMessage()
   mail.BodyFormat = MailFormat.Html
   mail.To = email
   mail.From = from
   mail.Subject = subject
   mail.Body = body
   mail.Bcc = "Events@xx.com"
   mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.xx.com"
   mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = "25" 'mail server port
   mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
   mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") ="sendemail@xx.com"
   mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="xxx"
   System.Web.Mail.SmtpMail.Send(mail)
mailit=" "

End Function

这是我在ASP.NET VB中转换

Public Shared Function MailIt(ByVal subject, ByVal from, ByVal email, ByVal body)

    Dim mail As New System.Net.Mail.MailMessage()
    mail.IsBodyHtml = True

    mail.Body = MailFormat.Html
    mail.To.Add(email)
    mail.From = from
    mail.Subject = subject
    mail.Body = body
    mail.Bcc = "Events@xx.com"
    mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.xx.com"
    mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = "25" 'mail server port
    mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
    mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "sendemail@xx.com"
    mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxx"
    System.Web.Mail.SmtpMail.Send(mail)
    MailIt = " "

End Function

1 个答案:

答案 0 :(得分:3)

尝试此操作并在web.config中设置SMTP设置

Public Shared Function MailIt(ByVal subject, ByVal from, ByVal email, ByVal body)

Dim mail As New System.Net.Mail.MailMessage()
mail.IsBodyHtml = True

mail.To.Add(email)
mail.From = from
mail.Subject = subject
mail.Body = body
mail.Bcc.Add("Events@xx.com")

Dim smtp As SmtpClient = New SmtpClient

smtp.Send(mailMessage)

End Function

Web.config设置

    <configuration>
   <system.net>
    <mailSettings>
     <smtp from="defaultEmail@yourdomain.com">
      <network host="smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/>
     </smtp>
    </mailSettings>
   </system.net>
</configuration>