c# - 多次发送邮件时邮件标题错误

标签 c# smtpclient

首先,如果这是一个重复的问题,我深表歉意。找了很多,都没有找到相关的issues。

问题来了:我正在使用 SmtpClient 和 MailMessage 类来发送邮件。我在邮件发送方法中将邮件的主题作为参数传递。第一次发送带有正确主题的邮件(我作为参数发送的邮件)。但是,在接下来的所有电子邮件中,无论我输入什么主题,主题都保持不变(第一次使用的主题)。主题是从方法内部设置的。

(注意:这是一个 WindowsForm 应用程序)

我尝试过的是,创建另一个名为“Refresh()”的方法,它处理邮件对象并再次创建它(仅使用 from 和 to 信息)。并在每次发送邮件后调用此方法。但这对解决这个问题没有帮助。

代码如下:

字段:

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

构造函数:

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*/ } 

刷新方法:

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

发送邮件的方法:

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

                mailer.Send(message);

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

调用示例:

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

现在无论在参数中发送什么主题,它总是发送主题“主题 1”(来自上面的示例)。邮件正文没问题,就是主题不对。

我在该类中几乎没有其他方法(用于其他目的,例如发送带附件的邮件),其中主题不是作为参数传递,而是直接从方法内部设置(如 message.Subject = "Example Sub" 从方法中),在这种情况下它工作正常。

但在上面的例子中,主题被传递给方法,主题保持不变。

最佳答案

如评论部分所述,没有理由缓存消息本身。目前,您正在处理消息(实际上将其置于不可用状态),然后重新创建它。查看更多HERE .您也可以简单地创建新对象并在完成后处理它们,以便垃圾收集器可以尽快释放资源。

只需使用一个简单的方法构造MailMessage并直接发送。

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);
}

然后:

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());

如果您在 MailMessage 中有附件,您应该在使用它们后调用 Dispose 来清除流。此外,在您使用完 SmtpClient 后调用 Dispose

关于c# - 多次发送邮件时邮件标题错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56440048/

相关文章:

asp.net-mvc-4 - 使用 SmtpClient 的 MVC 4.5 : Send an email with . pdf 附件 - 我在这里缺少什么?

c# - 无法发送给收件人 : c# exception

c# - 公共(public)字段与自动属性

c# - 将 C++ 控制台输出重定向到 C# 控制台不会打印任何内容

c# - 如何测试数值转换是否会改变值?

powershell - 当服务器响应为 5.7.0 时如何修复 'the SMTP server requires a secure connection or the client was not authenticated' 错误?

c#等待串口上的回显,检查它,等待超时

c# - Visual Studio C# 交换字符

vb.net - 发送邮件(交易失败。服务器响应为 : Relay rejected for policy reasons)

.NET SmtpClient : Is there a way to make sure that all emails resolve prior to sending MailMessage?