Reputation: 1
I use Mailkit in my contact form and everything works fine except filling the right mail from which is send to me. Every time the mail from and to is the same thing. The mail which I'm pointed out for my mail. I tried with static string instead income input's but the problem is still there. Can someone help me. I share the code below. Thank you. Best regards, Ventsislav
public class MailKitEmailSenderOptions
{
public MailKitEmailSenderOptions()
{
this.Host_SecureSocketOptions = SecureSocketOptions.Auto;
}
public SecureSocketOptions Host_SecureSocketOptions { get; set; }
public string Server { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
public class MailKitEmailSender : IEmailSender
{
public MailKitEmailSender(IOptions<MailKitEmailSenderOptions> options)
{
this.Options = options.Value;
}
public MailKitEmailSenderOptions Options { get; set; }
public Task SendEmailAsync(string email, string subject, string message)
{
return SendEmailAsync(email, subject, message);
}
public async Task SendEmailAsync(string from, string fromName, string subject, string htmlContent, IEnumerable<EmailAttachment> attachments = null)
{
try
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(fromName, from));
message.To.Add(new MailboxAddress(this.Options.Username));
message.Subject = subject;
message.Body = new TextPart("html")
{
Text = htmlContent,
};
using (var client = new SmtpClient())
{
client.Connect(this.Options.Server, this.Options.Port, this.Options.Host_SecureSocketOptions);
await client.AuthenticateAsync(this.Options.Username, this.Options.Password);
await client.SendAsync(message);
await client.DisconnectAsync(true);
}
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}
}
Upvotes: 0
Views: 1059
Reputation: 38538
If you are using an email server such as GMail, the server will replace the From header with whatever account you are using to send the message.
Upvotes: 1