电子邮件在Asp.net中发送,但生成错误 [英] Email is sent in Asp.net but an error is generated

查看:100
本文介绍了电子邮件在Asp.net中发送,但生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在向用户注册后向其发送注册确认电子邮件.该电子邮件是通过gmail smtp发送的.但是此后会生成错误,它是:
SMTP服务器需要安全连接,或者客户端未通过身份验证.
这是代码:

Hi all ,

I am sending a registration confirmation email to the user after he registers.The email is being sent through gmail smtp.But an error is generated after that and it is :
The SMTP server requires a secure connection or the client was not authenticated.
This is the code :

Try
           Dim user As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName.ToString())
           Dim userId As Guid = DirectCast(user.ProviderUserKey, Guid)
           Dim smtp As New SmtpClient()

           smtp.Host = "smtp.gmail.com"
           smtp.Port = 587
           smtp.EnableSsl = True
           smtp.DeliveryMethod = SmtpDeliveryMethod.Network
           smtp.UseDefaultCredentials = False
           smtp.Credentials = New NetworkCredential("ahmad.bastas@gmail.com", "password")
           ' Set MailMessage fields.
           e.Message.IsBodyHtml = True
           e.Message.Subject = "Allo Magazine : Registration Confirmation Email"
           ' Replace placeholder text in message body with information
           ' provided by the user.
           e.Message.Body = e.Message.Body.Replace("<%AC%>", userId.ToString())
           smtp.Send(e.Message)
           'after all of this happens we take the querystring when user presses on activation link and then see if he is not approved we approve him

       Catch ex As Exception
           Response.Write(ex.Message)

       End Try



这就是创建用户向导中的内容:



And this is what in the create user wizard :

<MailDefinition Priority="High" BodyFileName="~/ConfirmationEmailHTML/ConfirmationEmail.htm"

                       Subject="Allo Magazine : Registration Confirmation Email" From="ahmad.bastas@gmail.com">
                   </MailDefinition>




那是什么错误呢?

在此先谢谢您.




So what is the error ?

Thanks in advance.

推荐答案

尝试更改

try changing

smtp.UseDefaultCredentials = False







to

smtp.UseDefaultCredentials = True



并检查一下:

使用ASP.net通过Gmail帐户(Gmail SMTP服务器帐户)发送电子邮件 [ ^ ]

希望对您有所帮助:)



and check this out:

Sending E-mail using ASP.net through Gmail account (Gmail SMTP Server account)[^]

hope it helps :)


在设置网络凭据之前,将SSl设置为true

Before you set the network credential set the SSl to true

Dim SmtpClient As New Net.Mail.SmtpClient("smtp.gmail.com", 587 )
SmtpClient.EnableSsl = True 
'now provide network credential
smtp.Credentials = New NetworkCredential("ahmad.bastas@gmail.com", "password")


这篇关于电子邮件在Asp.net中发送,但生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆