Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

About getting Mail-MessageID in Vb.NET

friendprachifriendprachi Posts: 5Member
Hello,
I have created application which sents mail to users.
I want to get Mail MessageID of the mail sent to User.
How can I get Mail Message ID of the mail sent?

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Please provide information about how you are sending Mail Messages. Possibly sample code and what libraries you are using
  • friendprachifriendprachi Posts: 5Member
    Hello,
    I have written following code to send mail :-
    Public Function SendMail() As Boolean
    Try

    Dim ClientPC As New Net.Mail.SmtpClient(Domain, Port)
    Dim myAuthentication As New Net.NetworkCredential(MailFrom, Password)
    Dim sender As New MailAddress(MailFrom)
    Dim reciever As New MailAddress(MailTo)
    ClientPC.UseDefaultCredentials = False
    ClientPC.Credentials = myAuthentication
    If MailFrom.Contains("gmail") Then
    ClientPC.EnableSsl = True
    End If

    ' sEmail = gvMailContacts.GetRowCellDisplayText(rowno, "ContactID")
    Dim sMail As New MailMessage(sender, reciever)

    sMail.IsBodyHtml = True
    sMail.Subject = Subject
    sMail.Body = MailBody
    sMail.ReplyTo = sender

    If AttachmentPath <> "" Then
    Dim attach As New System.Net.Mail.Attachment(AttachmentPath)
    sMail.Attachments.Add(attach)
    End If

    ClientPC.Send(sMail)

    Return True


    Catch ex As Net.Mail.SmtpException
    Return False
    Catch ex As Exception
    Return False
    End Try

    End Function
    Now, if this function returns true I want to get the same mail which I have sent and its messageid .

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Try:
    [code]
    Public Function SendMail() As Net.Mail.MailMessage
    Try

    Dim ClientPC As New Net.Mail.SmtpClient(Domain, Port)
    Dim myAuthentication As New Net.NetworkCredential(MailFrom, Password)
    Dim sender As New Net.Mail.MailAddress(MailFrom)
    Dim reciever As New Net.Mail.MailAddress(MailTo)
    ClientPC.UseDefaultCredentials = False
    ClientPC.Credentials = myAuthentication
    If MailFrom.Contains("gmail") Then
    ClientPC.EnableSsl = True
    End If

    ' sEmail = gvMailContacts.GetRowCellDisplayText(rowno, "ContactID")
    Dim sMail As New Net.Mail.MailMessage(sender, reciever)

    sMail.IsBodyHtml = True
    sMail.Subject = Subject
    sMail.Body = MailBody
    sMail.ReplyTo = sender

    If AttachmentPath <> "" Then
    Dim attach As New System.Net.Mail.Attachment(AttachmentPath)
    sMail.Attachments.Add(attach)
    End If

    ClientPC.Send(sMail)

    Return sMail

    Catch ex As Net.Mail.SmtpException
    Return Nothing
    Catch ex As Exception
    Return Nothing
    End Try

    End Function
    [/code]

    When you call this function, you are checking the Boolean result to know if it sent successfully, now that you are returning a Net.Mail.MailMessage back, you will need to use the following code to know if it successfully sent:

    [code]
    Dim SentMail As Net.Mail.MailMessage
    SentMail = SendMail()

    'Here is where we check if it was successful
    If Not IsNothing(SentMail) Then
    'It sent successfully and SentMail contains the info you need
    Else
    'It didn't send, SentMail = Nothing and will give a NullReference
    'Error if you try to use it here
    End If
    [/code]

    Good Luck!
Sign In or Register to comment.