通过vb .net Windows应用程序发送电子邮件时出错(操作超时) [英] Getting an error (Operation Timed out) while sending an email via vb .net windows application

查看:96
本文介绍了通过vb .net Windows应用程序发送电子邮件时出错(操作超时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imports System.Web
Imports System.IO
Imports System.Net.Mail
Imports System.Net.Mail.Attachment
Imports System.Runtime
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                SendMail("alvikashif29@yahoo.com", "alvi_kashif@rocketmail.com", "Test Email", "Vb .net Appliction sent this mail", "smtp.gmail.com", False, 465, , "alvikashif29@gmail.com", "xxxxxxxxx")
    End Sub
    ''one static method for sending e-mails
    Public Sub SendMail(ByVal [From] As String, ByVal [To] As String, _
                        ByVal Subject As String, ByVal Body As String, ByVal MailServer _
                        As String, Optional ByVal IsBodyHtml As Boolean = True, _
                        Optional ByVal MailPort As Integer = 25, _
                        Optional ByVal Attachments() As String = Nothing, Optional _
                        ByVal AuthUsername As String = Nothing, Optional ByVal _
                        AuthPassword As String = Nothing)
        ''create a SmtpClient object to allow applications to send 
        ''e-mail by using the Simple Mail Transfer Protocol (SMTP).
        Dim MailClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(MailServer, MailPort)
        ''create a MailMessage object to represent an e-mail message
        ''that can be sent using the SmtpClient class
        Dim MailMessage = New System.Net.Mail.MailMessage([From], [To], Subject, Body)
        ''sets a value indicating whether the mail message body is in Html.
        MailMessage.IsBodyHtml = IsBodyHtml
        ''sets the credentials used to authenticate the sender
        If (AuthUsername IsNot Nothing) AndAlso (AuthPassword IsNot Nothing) Then
            MailClient.UseDefaultCredentials = False
            MailClient.EnableSsl = True
            MailClient.Credentials = New System.Net.NetworkCredential(AuthUsername, AuthPassword)
        End If
        ''add the files as the attachments for the mailmessage object
        If (Attachments IsNot Nothing) Then
            For Each FileName In Attachments
                MailMessage.Attachments.Add(New System.Net.Mail.Attachment(FileName))
            Next
        End If
        MailClient.Send(MailMessage)
        MessageBox.Show("mail sent")
    End Sub

End Class





这是我在新窗口窗体上的代码,点击事件上有一个按钮使用app调用sendmail ropriate参数但在运行时发生异常

操作已经超时

我正在使用我的gmail帐户

smtp服务器是= smtp.gmail.com

和ssl的端口是465

(现在不要告诉我使用587作为端口,因为它也不起作用。它给出了一个错误

SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要验证。)

请帮助请离开这种情况。



This my code on a new window form with a button on it on click event the sendmail is called with appropriate parameters but when running it an exception occurs
"The operation has timed out"
I am using my gmail account
the smtp server is = smtp.gmail.com
and the port for ssl is 465
(now dont tell me to use 587 as the port because it also does not works. It gives an error
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.")
Please help me out of this situation please.

推荐答案

你必须使用587端口。

关注我的答案从asp.net发送电子邮件到gmail [ ^ ]并检查您是否在做相同与否,否则格式化代码。



确保用户名和密码正确。



对于您获得的例外情况......

Google可能会阻止某个应用程序访问您的帐户

您可以在此处启用应用程序使用您的凭据访问Google帐户:

访问 https://accounts.google.com/DisplayUnlockCaptcha [ ^ ],单击继续按钮,然后尝试从应用程序发送邮件。
You have to use 587 port.
Follow my answer sending email to gmail from asp.net[^] and check whether you are doing the same or not, else format the code like that.

Make sure the username and password are correct.

For the exception you are getting...
Google may prevent an application from accessing your account
Here you can enable applications to access google account with your credentials:
Access https://accounts.google.com/DisplayUnlockCaptcha[^], click continue button and try to send mail after that from the application.


这篇关于通过vb .net Windows应用程序发送电子邮件时出错(操作超时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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