2

In the step by step guide at: https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

It is stated that the .net mail lib is used (System.Net.Mail).

In medical transactions, there is a need to change servers based on country region and record if the mail message was sent with status.

.net mail lib will do this but I have trouble understanding where to put the following code pieces when Using MVC Mailer:

.net Mail Lib-->

SmtpClient client = new SmtpClient(server, port);
client.credentials = CredentialCache.DefaultNetworkCredentials;

MVC Mailer-->

public ActionResult SendWelcomeMessage()
{
    UserMailer.SmtpClient(server, port);
    UserMailer.credentials = CredentialCache.DefaultNetworkCredentials;
    UserMailer.Welcome().SendAsync();


    return RedirectToAction("Index");
}

    static bool mailSent = false;
    private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
        // Get the unique identifier for this asynchronous operation.
         String token = (string) e.UserState;

        if (e.Cancelled)
        {
             Console.WriteLine("[{0}] Send canceled.", token);
        }
        if (e.Error != null)
        {
             Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
        } else
        {
            Console.WriteLine("Message sent.");
        }
        mailSent = true;
    }

if MailSent is false, then write to Critical Log Error.

I am not sure where the client setting for .net setting should go. Should they go in the controller as I have done above or in the Mailer method.

Thanks for any advice.

Regards, Vic

OpTech Marketing
  • 417
  • 2
  • 6
  • 19

1 Answers1

0

I had the same need.

To do so, i have created a custom mail sender class :

 Public Class CustomMailSender
        Inherits System.Net.Mail.SmtpClient
        Implements ISmtpClient       

    Public Sub Init(senderEmail As String, password As String)
        Me.Credentials = New System.Net.NetworkCredential(senderEmail, password)
    End Sub

    Public Overloads Sub SendAsync(mail As MailMessage) Implements ISmtpClient.SendAsync
        MyBase.SendAsync(mail, Nothing)
    End Sub

    Public Overloads Sub SendAsync(mail As MailMessage, userToken As Object) Implements ISmtpClient.SendAsync
        MyBase.SendAsync(mail, userToken)
    End Sub

    Public Overloads Sub Send(mail As MailMessage) Implements ISmtpClient.Send
        MyBase.Send(mail)
    End Sub

    Public Shadows Event SendCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Implements ISmtpClient.SendCompleted

End Class

Then inside your email controller, you use it like that

Public Class EmailController
        Inherits MailerBase

    Public Sub New()
        MyBase.New()
        Me.CustomMailSender = New CustomMailSender
    End Sub

    Public Property CustomMailSender As CustomMailSender

    Public Sub Sample()
        Dim mvcMailMessage As MvcMailMessage = Populate(Sub(i)
                                                            i.ViewName = "Sample"
                                                            i.To.Add("some1@somewhere.org")
                                                            i.Subject = "Boo!"
                                                        End Sub)
        mvcMailMessage.Send(Me.CustomMailSender)
    End Sub
End Class

Yeah, i know that's VB, but i'm a VB guy ! :> Hope this helps :)

OK cowboy
  • 65
  • 1
  • 10