伊莉討論區

標題: mail寄信 連接安全性問題 [打印本頁]

作者: fenghsite    時間: 2015-3-25 10:30 PM     標題: mail寄信 連接安全性問題

小弟需要使用到mail寄信的部分
以下為小弟的程式碼
Try
            Dim myMail As New MailMessage()
            myMail.From = New MailAddress("~~@gmail.com", "~~") '發送者
            myMail.To.Add("fuchyunbih@gmail.com") '收件者
            myMail.Subject = "測試郵件" '主題
            myMail.Body = "aa~" '內文

            Dim mySmtp As New SmtpClient() '建立SMTP連線
            mySmtp.Credentials = New System.Net.NetworkCredential("~~@gmail.com", "~~") '連線驗證
            mySmtp.Port = 587 'SMTP Port
            mySmtp.Host = "smtp.gmail.com" 'SMTP主機名稱
            mySmtp.EnableSsl = True '開啟SSL驗證
            mySmtp.Send(myMail) '發送
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
我已經有開啟SSL驗證了  但是google還是不能登入寄信信箱也會收到 "Google 帳戶:登入嘗試遭拒" 的信件
[attach]107868633[/attach]
我去把圖片中這個開啟  就可以成功寄信了
但是小弟需要使用者自己的帳號可以登入
不可能讓使用者都更改這個
請問有什麼方法可以寄信的嗎Q___Q
謝謝大大

作者: darkjack    時間: 2015-3-26 07:22 PM

不是很懂你那個 安全性的 開關在哪
我用win 7 64 bit 一切內定設定(除了瀏覽器之外)
我寫的一小段 並沒有這樣的問題喔
textbox 請自己增加喔
  1.   Dim mail As New MailMessage()
  2.   Dim SmtpServer As New SmtpClient()
  3.     SmtpServer.Credentials = New Net.NetworkCredential(TextBox2.Text, TextBox5.Text)
  4.     SmtpServer.Port = 587
  5.     SmtpServer.Host = "smtp.gmail.com"
  6.     SmtpServer.EnableSsl = True

  7.     mail = New MailMessage()
  8.     '多郵件分隔
  9.     Dim addr() As String = TextBox1.Text.Split(",")

  10.     Try
  11.       '顯示 郵件地址 , 暱稱 , 文件格式
  12.       mail.From = New MailAddress(TextBox2.Text, TextBox6.Text, System.Text.Encoding.UTF8)

  13.       Dim i As Byte
  14.       For i = 0 To addr.Length - 1
  15.         '郵件集合(多人一次送出)
  16.         'mail.To.Add(addr(i))
  17.         'mail.CC.Add(addr(i))
  18.         mail.Bcc.Add(addr(i))
  19.       Next

  20.       mail.Subject = TextBox3.Text
  21.       mail.Body = TextBox4.Text

  22.       '檔案列表是否有檔案
  23.       If ListBox1.Items.Count <> 0 Then
  24.         For i = 0 To ListBox1.Items.Count - 1
  25.           mail.Attachments.Add(New Attachment(ListBox1.Items.Item(i)))
  26.         Next
  27.       End If
  28.       mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
  29.       mail.ReplyTo = New MailAddress(TextBox1.Text)
  30.       SmtpServer.Send(mail)
  31.       MsgBox("OK")
  32.       '清除列表
  33.       ListBox1.Items.Clear()
  34.     Catch ex As Exception
  35.       MsgBox(ex.ToString())
  36.     End Try
複製代碼




作者: johnwanz    時間: 2015-3-27 09:52 AM

http://jimmysu.logdown.com/posts ... entication-required

依照此網頁所說, 該功能鎖定是在個人google帳戶的安全性設定中, 正常流程應該是沒辦法從應用程序修改的.
作者: Jeepluo    時間: 2015-3-28 07:32 PM

這裡有一段程式,供參考看看
  1. Imports System.IO
  2. Imports EAGetMail 'imports EAGetMail namespace

  3. Module Module1
  4.     Sub Main()
  5.         ' Create a folder named "inbox" under current directory
  6.         ' to save the email retrieved.
  7.         Dim curpath As String = Directory.GetCurrentDirectory()
  8.         Dim mailbox As String = [String].Format("{0}\inbox", curpath)

  9.         ' If the folder is not existed, create it.
  10.         If Not Directory.Exists(mailbox) Then
  11.             Directory.CreateDirectory(mailbox)
  12.         End If
  13.             
  14.         ' Gmail IMAP server is "imap.gmail.com"
  15.         Dim oServer As New MailServer("imap.gmail.com", _
  16.             "gmailid@gmail.com", "yourpassword", ServerProtocol.Imap4 )
  17.         Dim oClient As New MailClient("TryIt")

  18.         ' Enable SSL connection
  19.         oServer.SSLConnection = True

  20.         ' Set IMAP4 SSL port
  21.         oServer.Port = 993

  22.         Try
  23.             oClient.Connect(oServer)
  24.             Dim infos As MailInfo() = oClient.GetMailInfos()
  25.             For i As Integer = 0 To infos.Length - 1
  26.                 Dim info As MailInfo = infos(i)
  27.                 Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}", _
  28.                         info.Index, info.Size, info.UIDL)

  29.                 ' Receive email from Gmail server
  30.                 Dim oMail As Mail = oClient.GetMail(info)

  31.                 Console.WriteLine("From: {0}", oMail.From.ToString())
  32.                 Console.WriteLine("Subject: {0}" & vbCr & vbLf, oMail.Subject)

  33.                 ' Generate an email file name based on date time.
  34.                 Dim d As System.DateTime = System.DateTime.Now
  35.                 Dim cur As New System.Globalization.CultureInfo("en-US")
  36.                 Dim sdate As String = d.ToString("yyyyMMddHHmmss", cur)
  37.                 Dim fileName As String = [String].Format("{0}\{1}{2}{3}.eml", _
  38.                      mailbox, sdate, d.Millisecond.ToString("d3"), i)

  39.                 ' Save email to local disk
  40.                 oMail.SaveAs(fileName, True)

  41.                 ' Mark email as deleted in Gmail Account
  42.                 oClient.Delete(info)
  43.             Next

  44.             ' Quit and pure emails marked as deleted from Gmail IMAP4 server.
  45.             oClient.Quit()
  46.         Catch ep As Exception
  47.             Console.WriteLine(ep.Message)
  48.         End Try

  49.     End Sub
  50. End Module
複製代碼

作者: fenghsite    時間: 2015-3-30 11:00 PM

本帖最後由 fenghsite 於 2015-3-30 11:01 PM 編輯
darkjack 發表於 2015-3-26 07:22 PM
不是很懂你那個 安全性的 開關在哪
我用win 7 64 bit 一切內定設定(除了瀏覽器之外)
我寫的一小段 並沒有 ...

我用大大的程式碼gmail也是要開啟"安全性較低的應用程式"不然也是ERROR
[attach]107952870[/attach]
如果寄件者是用yahoo的就可以了
這種安全性的問題有解嗎ˊ___ˋ
我還有一個google帳號有2階段手機驗證
也是一樣需要安全連接.....
謝謝大大分享的程式碼
作者: darkjack    時間: 2015-4-1 08:16 AM

一般來說 就只能手動開關吧...只是一般人好像不會動用到那邊吧?




歡迎光臨 伊莉討論區 (http://a401.file-static.com/) Powered by Discuz!