自动向登录用户发送邮件

时间:2014-09-12 06:28:17

标签: c# email

我真的很沮丧,很抱歉,如果我问一些显而易见的东西,但我正在尝试发送自动邮件,每次有人输入他们的用户名来访问我的程序,但它给了我一个错误,说这是不是有效的地址。 Login_name就像john.doe,我希望它能自动添加到“@ mail.com”前面。这是错的吗,我需要另一种方法吗?

提前致谢。

mail.To.Add(login_name+"@mail.com");

1 个答案:

答案 0 :(得分:0)

试试这个

    public bool Email(this string body, string subject, string sender, string recipient, string server)
    {
        try
        {
            // To
            MailMessage mailMsg = new MailMessage();
            mailMsg.To.Add(recipient);

            // From
            MailAddress mailAddress = new MailAddress(sender);
            mailMsg.From = mailAddress;

            // Subject and Body
            mailMsg.Subject = subject;
            mailMsg.Body = body;

            // Init SmtpClient and send
            SmtpClient smtpClient = new SmtpClient(server);
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
            smtpClient.Credentials = credentials;

            smtpClient.Send(mailMsg);
        }
        catch (Exception ex)
        {
            throw new Exception("Could not send mail from: " + sender + " to: " + recipient + " thru smtp server: " + server + "\n\n" + ex.Message, ex);
        }

        return true;
    }

显然你必须在这里和那里进行编辑但是你明白了