Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

Is it possible to have a different sender email and configuration email to send email from MimeKit in ASP.Net Core?

I have added code which will send emails to the receiving party. As of now I need to add the same email address in "from" email and "username"(config email) to send the email else it will fail. But I wish to have different "from" email and not asking users for password and using one config mail to send email so multiple users can consume this service to send emails. Is it possible to do so?

Here is my code:

public async Task<string> Send(string from, string to, string subject, string html, string userName, string password)
{
    // create message
    var email = new MimeMessage();
    email.From.Add(MailboxAddress.Parse(from));
    email.To.Add(MailboxAddress.Parse(to));
    email.Subject = subject;
    email.Body = new TextPart(TextFormat.Html) { Text = html };
    using var smtp = new SmtpClient();
    smtp.Connect("smtp.live.com", 587, SecureSocketOptions.StartTls);
    smtp.Authenticate(userName, password);
    smtp.Send(email);
    smtp.Disconnect(true);
    return "Email Set";
}
question from:https://stackoverflow.com/questions/65650378/is-it-possible-to-have-a-different-sender-email-and-configuration-email-to-send

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, but you'll need to use a private SMTP server that will accept different addresses in the From: header and the authenticate commands. None of the widely-used public SMTP servers allow this as far as I know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...