Marwa Almahmoud Marwa Almahmoud - 2 months ago 28
C# Question

Send Email by hangfire without using <mailSettings> in webconfig

I worked with mvc 5
I want to send email by hangfire without get data from
In webconfig I have this code:

<smtp from="">
<network host="host" port="25" userName="" password="*****" enableSsl="false" defaultCredentials="false" />

when I use in Webconfig ,everything works correctly.
But now I want to store email and password in database and get the value from database.

this is .cs code

[AutomaticRetry(Attempts = 20)]
public static void SendEmailToMember(Member obj,string Subject, string Details){
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));

var emailService = new Postal.EmailService(engines);

var ee = new SendEmailToMember
To = obj.Email,
FullName = obj.FullName,
Details = Details,
ViewName = "SendEmailToMember"



How I can change it to get mail settings from database?

mww mww

You can use SmtClient for create new Postal.EmailService, Import the System.Net.Mail namespace and use this code

        MailMessage mail = new MailMessage();

        SmtpClient smtpServer = new SmtpClient(""); //gmail smtp server
        smtpServer.Credentials = new System.Net.NetworkCredential("loginFromDB", "passwordFromDB");
        smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpServer..EnableSsl = false;
        smtpServer.Port = 587; // gmail works on this port

        mail.From = new MailAddress("");
        mail.Subject = "Hello from the other side";
        mail.Body = "your body";

        Postal.EmailService emailService = new Postal.EmailService(new ViewEngineCollection(), () => smtpServer);