2015年12月21日 星期一

vbnet gmail 寄信

相信很多人應用程式開發均採用gmail 當主發!
現gmail 預設均採用較高的安全性,若使用的帳號沒有開放應用程式發信功能;怎發也是發不出去的!
今天也是要發,狂碰丁子!! = . =
總算有看到別人也有說明問題點在哪!!
底下為程式範例:
-----------------
(1)
Dim smtpServer As New SmtpClient
Dim mail As New MailMessage()
Dim mailAttachment As Attachment = New Attachment("XMLFile")
smtpServer.UseDefaultCredentials = False
smtpServer.Credentials = New Net.NetworkCredential("a@ymail.com", "12345")
smtpServer.Port = 465
smtpServer.EnableSsl = True
smtpServer.Host = "smtp.mail.yahoo.com"

mail = New MailMessage()
mail.From = New MailAddress("a@ymail.com")
mail.To.Add("a@ymail.com")
mail.Subject = "110BackUp"
mail.IsBodyHtml = False
mail.Body = "XML FILE"
mail.Attachments.Add(mailAttachment)
smtpServer.Send(mail)
-----------------
(2)
Imports System.Net.Mail
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New _
Net.NetworkCredential("username@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("YOURusername@gmail.com")
            mail.To.Add("TOADDRESS")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
-----------------
讓gmail 可以發信的設定(解決方法):










沒有留言:

張貼留言