none
Mail.mailaddress issue RRS feed

  • Question

  • Hello all,

    mensaje.from = New mail.mailaddress(enviara)

    mensaje.to.add(quien)

    I have a program that sends emails when I run it the first time, it works fine. If I run it a second time, it sends an email to the new address and to the old address too.

    Is there a way to reset this so the From, To and BCC starts empty every time I try to send a new email

    Thanks







    • Edited by Jrarias1776 Wednesday, December 26, 2018 2:06 PM
    Wednesday, December 26, 2018 1:55 PM

All replies

  • Hi

    Post is unreadable. Can you please fix.


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, December 26, 2018 2:10 PM
    Wednesday, December 26, 2018 1:57 PM
  • mensaje.from = New mail.mailaddress(enviara)

    mensaje.to.add(quien)

    every time that I run it, it gives me that error, but if I close it and open it again, it works fine. I guess I need to restart the Mailaddress, To, BCC but I don't know how to do it

    Wednesday, December 26, 2018 2:05 PM
  • mensaje.from = New mail.mailaddress(enviara)

    mensaje.to.add(quien)

    every time that I run it, it gives me that error, but if I close it and open it again, it works fine. I guess I need to restart the Mailaddress, To, BCC but I don't know how to do it

    Hi

    Guessing here:

    1. you say works fine first time but not when you run it again. Do you mean that the application has been completely shut down yet retains the remnants of a previously entered email address? I can't see how that can happen unless you are explicitely setting the address each time you run the application.

    2. more likely, you mean that while the application is still running, you send an email and the next one sends to old address?

    *

    If 1. then I have no idea.

    *

    If 2. then maybe you need to clear/reset the mensaje variable prior to re-using for second (and subsequent) emails. For example, just before the code where you fill mensaje with the various data, re-declare it.


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, December 26, 2018 2:21 PM
    • Proposed as answer by dbasnett Thursday, December 27, 2018 2:42 PM
    Wednesday, December 26, 2018 2:20 PM
  • Hello,

    For each email sent you typically create MailMessage and SmtpClient each time and optionally setup SendCompleted delegate.

    Here is a super simple example that of course can take many directions but the constant is creating MailMessage and the SmtpClient objects each time a message is sent.

    So that would be the reset.

    Imports System.ComponentModel
    Imports System.Net.Mail
    
    Public Class MailExample
        Public Sub Sender()
            Dim mail As New MailMessage With {
                .From = New MailAddress("me@mycompany.com", "Steve James")
            }
    
            mail.To.Add("you@yourcompany.com")
            mail.To.Add("you2@yourcompany.com")
    
            mail.CC.Add("cc1@yourcompany.com")
            mail.CC.Add("cc2@yourcompany.com")
    
            mail.Bcc.Add("blindcc1@yourcompany.com")
            mail.Bcc.Add("blindcc2@yourcompany.com")
    
            'set the content
            mail.Subject = "This is an email"
            mail.Body = "this is the body content of the email."
    
    
            Dim client As New SmtpClient("TODO")
            AddHandler client.SendCompleted, AddressOf SendCompletedCallback
            Dim userState As String = "test message1"
            client.SendAsync(mail, userState)
    
        End Sub
        Private Shared mailSent As Boolean = False
        Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
    
            ' Get the unique identifier for this asynchronous operation.
            Dim token = CStr(e.UserState)
    
            If e.Cancelled Then
                Console.WriteLine("[{0}] Send canceled.", token)
            End If
            If e.Error IsNot Nothing Then
                Console.WriteLine("[{0}] {1}", token, e.Error.ToString())
            Else
                Console.WriteLine("Message sent.")
            End If
    
            mailSent = True
        End Sub
    End Class
    
    There are many examples on the following page


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by dbasnett Thursday, December 27, 2018 2:42 PM
    Wednesday, December 26, 2018 2:43 PM
    Moderator
  • Hello,

    I re-declare it and it works. Thanks for your help

    Wednesday, December 26, 2018 4:14 PM
  • Hi,

    We appreciate that you shared your solution with us. Please mark the post(s) that helped you as the answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 27, 2018 5:22 AM