pherbio
pherbio

Reputation: 45

Sending email from web site using Google Apps account

Been researching this issue for quite a while, but have yet to find any solution.

I can send email from the website using my regular gmail account using smtp.gmail.com as my host and port 587.

My current problem is that there's no problem sending the mail. I no longer receive an error. However, the email is never sent. Anyone have any ideas?

Here's the code:

Config:

<smtp from="[email protected]">
<network host="smtp.gmail.com" password="password" userName="[email protected]" port="587"/>
</smtp>

Code:

    public void Send() {

        bool bDev = ConfigurationManager.AppSettings["dev"] == "true";

        MailMessage oMsg = new MailMessage();

        foreach (string sAddress in To) {
                if (sAddress != "") oMsg.To.Add(sAddress);
        }

        oMsg.From = ((FromName == null) || (FromName == "")) ?
                    new MailAddress(From) :
                    new MailAddress(From, FromName);
        oMsg.Subject = Subject;

        oMsg.Body = Body.ToString();
        oMsg.IsBodyHtml = true;

        oMsg.BodyEncoding = Encoding.UTF8;
        SmtpClient smtp = new SmtpClient();
        smtp.EnableSsl = (new int[] { 587, 465 }).Contains(smtp.Port);
        smtp.Send(oMsg);
    }

Upvotes: 0

Views: 200

Answers (2)

pherbio
pherbio

Reputation: 45

I added a second To address and resumed testing. I began to receive email to both accounts. Then after removing the second address, it started working.

I ran into a new issue where my reply-to address was using the From address. So, I added additional code to set the reply address to that of the person's email address from my form.

Code

    if (!string.IsNullOrEmpty(ReplyToAddress))
        oMsg.ReplyTo = new MailAddress(ReplyToAddress);

However, I ran into an issue with Gmail ignoring my defined reply-to address, and using my from address. Apparently this is by design. I couldn't find any useful information on how to override this setting, so instead I did a workaround that might prove to be helpful to anyone else having this issue.

I created a generic email address with gmail ([email protected]) and setup a filter. Any emails coming from [email protected] need to be redirected to [email protected].

Now, when I run my tests, all the emails are going where they are supposed to go AND the reply to field works great.

Not the best solution as it involves me remembering to setup a new account every time I run into this issue, but it's a solution that works for the time being until a better alternative comes up.

Upvotes: 1

realnero
realnero

Reputation: 994

Had exactly same issue, that no error happened but also no email was sent. Google sometimes blocks your account(usually during testing). Try logging in into gmail account and see what it says.

UPD1: also check your sent folder in gmail.

UPD2: Also make sure that "From" email is the same as your gmail email address.

Upvotes: 0

Related Questions