Gmail setup for email settings

Mar 28, 2012 at 5:14 PM

I successfully installed the blogengine.net  and it is running quite well, however, I cannot for the life of me get the email settings to accept my smtp GMail account.

My host is Network solutions. I have other .Net apps on my website that I have coded and I am able to send out email through GMAIL smtp so I am confused why I cannot on the blogEngine.Net

I have tried all of the solutions I have found on google search and even on this discussion threads.

I have tried modifying the webconfig file, I have tried varius port settings and all but to no avail.

My settings as of now:

smtp.gmail.com

port 587

Enable SSL

Can someone help me please.

Coordinator
Mar 28, 2012 at 7:16 PM

Usually this happens because hosts block ports like 587 etc. But if you have other apps using SMTP with Gmail, try to modify function that sends mail. You can for simplicity add a test page with button click, add this function and call it. You can modify it to use hardcoded values, output errors and so on. At the end, it is just a simple SMTP call. If it works for other apps, it should work here. Also I would try it locally first. 

public static string SendMailMessage(MailMessage message)
{
	if (message == null)
	{
		throw new ArgumentNullException("message");
	}

	StringBuilder errorMsg = new StringBuilder();

	try
	{
		message.IsBodyHtml = true;
		message.BodyEncoding = Encoding.UTF8;
		var smtp = new SmtpClient(BlogSettings.Instance.SmtpServer);

		// don't send credentials if a server doesn't require it,
		// linux smtp servers don't like that 
		if (!string.IsNullOrEmpty(BlogSettings.Instance.SmtpUserName))
		{
			smtp.Credentials = new NetworkCredential(
				BlogSettings.Instance.SmtpUserName, BlogSettings.Instance.SmtpPassword);
		}

		smtp.Port = BlogSettings.Instance.SmtpServerPort;
		smtp.EnableSsl = BlogSettings.Instance.EnableSsl;
		smtp.Send(message);
		OnEmailSent(message);
	}
	catch (Exception ex)
	{
		OnEmailFailed(message);

		errorMsg.Append("Error sending email in SendMailMessage: ");
		Exception current = ex;

		while (current != null)
		{
			if (errorMsg.Length > 0) { errorMsg.Append(" "); }
			errorMsg.Append(current.Message);
			current = current.InnerException;
		}

		Utils.Log(errorMsg.ToString());
	}
	finally
	{
		// Remove the pointer to the message object so the GC can close the thread.
		message.Dispose();
	}

	return errorMsg.ToString();
}

Mar 28, 2012 at 7:21 PM
Edited Mar 28, 2012 at 7:22 PM

Thank you for responding :)

I'm sorry if I don't fully understand... you want me to put this code into what file on BlogEngine.NET?

Coordinator
Mar 29, 2012 at 4:24 AM
Edited Mar 29, 2012 at 4:25 AM

First thing you need is to make sure you run application in full trust - most host won't let SMTP run on medium trust. So make sure you don't have <trust level="Medium"> in your web.config. It should be full.

If that didn't fix issue and you familiar with c#, you can try to create file called test.aspx in application root directory, no code behind, and copy/paste this code into that file:

 

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { try { var message = new MailMessage { From = new MailAddress("you@gmail.com"), Subject = "Test" }; message.To.Add("YOURREALEMAIL@gmail.com"); message.Body = "This is a test"; var smtp = new SmtpClient("smtp.gmail.com"); smtp.Credentials = new NetworkCredential("YOURREALEMAIL@gmail.com", "YOURREALPASSWORD"); smtp.Port = 587; smtp.EnableSsl = true; smtp.Send(message); lblMessage.Text = "Completed!"; } catch (Exception ex) { lblMessage.Text = ex.Message; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Height="48px" Text="Send mail" Width="216px" onclick="Button1_Click" /> <asp:Label ID="lblMessage" runat="server" Text="" /> </div> </form> </body> </html>

 

Now navigate to yoursite/test.aspx and click button to send email. You can modify code in button click to try different values and check any errors that will be displayed in the label. When I run this code locally replacing dummy values with my gmail user/password, I can send myself emails just fine.

Mar 29, 2012 at 4:21 PM
Edited Mar 29, 2012 at 4:49 PM

Thanks again for replying... I was able to send email only after I got rid of the smtp.Port = 587 section from the code you provided in the test.aspx. I assume that it is the port that is causing the issue then. If that is truly the case then how do I modify the blogengine.net to not use the port? What file is that setting in?

I tried modifying the email.aspx and email.aspx.cs under admin settings by removing port info lines but that didn't work get same error.

Thanks

Coordinator
Mar 29, 2012 at 5:10 PM

Try port 25, this is a default. If it works, go to admin -> settings -> e-mail and set port number to 25.

Mar 29, 2012 at 5:21 PM

I put port 25 in the test.aspx and it worked however when I put port 25 in the admin settings email it still gives the same error. Is there some other settings in BlogEngine.net that controls the behaviour? obviously the test.aspx proves gmail works with my hosting it just seems that there is a setting in BlogEngine.net that is not allowing it.

Mar 29, 2012 at 5:39 PM

Ok I deleted my browser cache... put in port 25 on admin settings email and hit the save button. I then hit another admin page then returned to the email page. I selected the test button and it now says the test was successful. I went to the contact page and tested it out and it worked :) So now it works and I believe we can rest this case... port 25 with enable ssl for Gmail on Network solutions Hosting works Not port 587 or 465 as suggested by others even GOOGLE.

Thanks for your help :)

Coordinator
Mar 29, 2012 at 6:02 PM

Cool, glad it worked out for you :)