2011-11-07 12 views
0

以下のコードには何がありますか?電子メールメッセージを送信できません。vb.netがメールを送信していません

Private Sub sendTestEmail() 

Dim EmailBody As String 

EmailBody = "This is a test *****************" 


Dim TestEmail As New System.Net.Mail.MailMessage("[email protected]", "[email protected]", "TestEmail", EmailBody) 

Dim EmailServer As New System.Net.Mail.SmtpClient("localhost") 
EmailServer.SendAsync(TestEmail, Me) 

End Sub 
+3

メッセージはなんですか?エラーの詳細は重要です。 – David

+2

あなたのボックスがsmptpサーバとして適切に設定されていない可能性があります。 Widnowsを使用している場合は、InetPub \ mailrootに移動します。あなたの電子メールはすべて送信されるのを待っていると確信しています – Icarus

+0

localhostでSMTPサーバを実行していますか? –

答えて

2

はあなたのコードがhere

から来たようにそれはあなたのSMTPサーバーをインストールまたはローカルホスト上に構成されていないことをおそらくあるルックス。

ローカルホストのSMTPサーバーがメールを転送しないようにすることがセキュリティ上の問題となる可能性があります。

0

私はこのタスクを簡単に実行できるクラスを作成しました。

Imports System.Net.Mail 
Public Class GGSMTP_GMAIL 
    Dim Temp_GmailAccount As String 
    Dim Temp_GmailPassword As String 
    Dim Temp_SMTPSERVER As String 
    Dim Temp_ServerPort As Int32 
    Dim Temp_ErrorText As String = "" 
    Dim Temp_EnableSSl As Boolean = True 
    Public ReadOnly Property ErrorText() As String 
     Get 
      Return Temp_ErrorText 
     End Get 
    End Property 
    Public Property EnableSSL() As Boolean 
     Get 
      Return Temp_EnableSSl 
     End Get 
     Set(ByVal value As Boolean) 
      Temp_EnableSSl = value 
     End Set 
    End Property 
    Public Property GmailAccount() As String 
     Get 
      Return Temp_GmailAccount 
     End Get 
     Set(ByVal value As String) 
      Temp_GmailAccount = value 
     End Set 
    End Property 
    Public Property GmailPassword() As String 
     Get 
      Return Temp_GmailPassword 
     End Get 
     Set(ByVal value As String) 
      Temp_GmailPassword = value 
     End Set 
    End Property 
    Public Property SMTPSERVER() As String 
     Get 
      Return Temp_SMTPSERVER 
     End Get 
     Set(ByVal value As String) 
      Temp_SMTPSERVER = value 
     End Set 
    End Property 
    Public Property ServerPort() As Int32 
     Get 
      Return Temp_ServerPort 
     End Get 
     Set(ByVal value As Int32) 
      Temp_ServerPort = value 
     End Set 
    End Property 
    Public Sub New(ByVal GmailAccount As String, ByVal GmailPassword As String, Optional ByVal SMTPSERVER As String = "smtp.gmail.com", Optional ByVal ServerPort As Int32 = 587, Optional ByVal EnableSSl As Boolean = True) 
     Temp_GmailAccount = GmailAccount 
     Temp_GmailPassword = GmailPassword 
     Temp_SMTPSERVER = SMTPSERVER 
     Temp_ServerPort = ServerPort 
     Temp_EnableSSl = EnableSSl 
    End Sub 
    Public Function SendMail(ByVal ToAddressies As String(), ByVal Subject As String, ByVal BodyText As String, Optional ByVal AttachedFiles As String() = Nothing) As Boolean 
     Temp_ErrorText = "" 
     Dim Mail As New MailMessage 
     Dim SMTP As New SmtpClient(Temp_SMTPSERVER) 
     Mail.Subject = Subject 
     Mail.From = New MailAddress(Temp_GmailAccount) 
     SMTP.Credentials = New System.Net.NetworkCredential(Temp_GmailAccount, Temp_GmailPassword) '<-- Password Here 
     Mail.To.Clear() 
     For i As Int16 = 0 To ToAddressies.Length - 1 
      Mail.To.Add(ToAddressies(i)) 
     Next i 
     Mail.Body = BodyText 
     Mail.Attachments.Clear() 

     If AttachedFiles IsNot Nothing Then 
      For i As Int16 = 0 To AttachedFiles.Length - 1 
       Mail.Attachments.Add(New Attachment(AttachedFiles(i))) 
      Next 
     End If 

     SMTP.EnableSsl = Temp_EnableSSl 
     SMTP.Port = Temp_ServerPort 

     Try 
      SMTP.Send(Mail) 
      Return True 
     Catch ex As Exception 
      Me.Temp_ErrorText = ex.Message.ToString 
      Return False 
     End Try 

    End Function 
End Class 

クラスを使用する方法、その方法、:

Dim GGmail As New GGSMTP_GMAIL("[email protected]", "AccPassword",) 

     Dim ToAddressies As String() = {"[email protected]", "[email protected]"} 
     Dim attachs() As String = {"d:\temp_Excell226.xlsx", "d:\temp_Excell224.xlsx", "d:\temp_Excell225.xlsx"} 
     Dim subject As String = "My TestSubject" 
     Dim body As String = "My text goes here ...." 
     Dim result As Boolean = GGmail.SendMail(ToAddressies, subject, body, attachs) 
     If result Then 
      MsgBox("mails sended successfully", MsgBoxStyle.Information) 
     Else 
      MsgBox(GGmail.ErrorText, MsgBoxStyle.Critical) 
     End If 

は、この情報がお役に立てば幸いです。良いコーディング

関連する問題