wannaknow
wannaknow

Reputation: 39

Wrong email titles when sending mails multiple times

First of all, my apologies if this is a duplicate question. I have searched for it a lot, but couldn't find related issues.

So here's the problem: I am using SmtpClient and MailMessage class to send mails. I am passing the subject of the mail as a parameter in the mail sending method. First time the mail is sent with the proper subject (the one i sent as parameter). However, in all next emails, no matter what subject i put, the subject remains the same (the one used first time). The subject is set from inside of the method.

(Note: This is a WindowsForm application)

What i have tried is, creating another method named "Refresh()" which disposes the mail object and creates it again (with from and to info only). And call this method each time after a mail is sent. But it doesn't help with this problem.

Codes are given below:

Fields:

MailMessage message;
SmtpClient mailer;
string from = "sender email";
string pass = "sender pass";
string to = "rec email";

Constructor:

try
{
    message = new MailMessage(from, to);

    mailer = new SmtpClient("smtp.gmail.com", 587);
    mailer.Credentials = new NetworkCredential(from, pass);
    mailer.EnableSsl = true;
}
catch(Exception ex) { /*code to write log*/ } 

Refresh method:

void RefreshMessage()
        {
            try
            {
                message.Subject = "";
                message.Dispose();
                message = new MailMessage(from, to);
            }
            catch(Exception ex) { /*write log*/ }
        }

Method which is sending the mail:

internal void TextOnly(string sub, string bodyMessage)
        {
            try
            {
                message.Subject = sub;
                message.Body = bodyMessage;

                mailer.Send(message);

                this.RefreshMessage();
            }
            catch (Exception ex) { /*write log*/ }
        }

Example of how it's called:

m.TextOnly("Subject 1" , SomeStringMethod());
m.TextOnly("Another Title " + anyString, "Some string mail");
m.TextOnly("[TAG] Email subject goes here" , AnotherStringMethod());

Now no matter whatever subject is sent in the parameter, it will always send with subject "Subject 1" (from the example above). The body of the message is fine, only the subject is not right.

I have few other methods in the class (for other purposes like sending mails with attachments for example), where subject isn't passed as parameter but it's set directly from within the method (like message.Subject = "Example Sub" from within the method), in that case it works fine.

But in the case above, where the subject is passed to the method, the subject remains the same.

Upvotes: 3

Views: 764

Answers (2)

zhulien
zhulien

Reputation: 5695

Like the comment section already stated, there is no reason to cache the message itself. Currently, you're disposing the message(which actually puts it in a unusable state) and then you recreate it. Check out more HERE. You can just as well simply create new objects and dispose of them after you're done so the Garbage Collector can release the resources as soon as possible.

Just utilize a simple method for constructing MailMessages and send them directly.

internal MailMessage ConstructTextMailMessage(MailAddress from, MailAddress to, string subject, string body)
{
    return ConstructTextMailMessage(from.Address, to.Address, subject, body);
}

internal MailMessage ConstructTextMailMessage(string from, string to, string subject, string body)
{
    return new MailMessage(from, to, subject, body);
}

And then:

var mailClient = new SmtpClient("smtp.gmail.com", 587);

mailClient.Credentials = new NetworkCredential(from, pass);
mailClient.EnableSsl = true;

mailClient.Send(ConstructTextMailMessage(from, to, "Subject 1", SomeStringMethod()));
mailClient.Send(ConstructTextMailMessage(from, to, "Another Title " + anyString, "Some string mail");
mailClient.Send(ConstructTextMailMessage(from, to, "[TAG] Email subject goes here", AnotherStringMethod());

If you have attachments in the MailMessage, you should call Dispose after using them to clear up the streams. Also, call Dispose on the SmtpClient when you're done using it.

Upvotes: 2

Kiwimanshare
Kiwimanshare

Reputation: 150

I used the same functionality (SntpClient, MailMessage etc.) in one of my programms and it worked just fine:

SmtpClient client = new SmtpClient("host", port);
MailMessage mail;
MailAddress absender = new MailAddress("[email protected]");

foreach (string sub in Subjects)
{
    mail = new MailMessage();
    mail.IsBodyHtml = true;
    mail.Subject = sub;
    mail.From = absender;

    mail.To.Add("[email protected]");

    client.Send(mail);
}

Mybe you just need to make a new MailMessage-Object each time you "create" a E-Mail.

Upvotes: 1

Related Questions