使用C#通过Gmail SMTP服务器发送电子邮件 [英] Sending email through Gmail SMTP server with C#

查看:416
本文介绍了使用C#通过Gmail SMTP服务器发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,接受的答案或其他任何其他工作都不适用于通过Gmail发送电子邮件。为什么他们不工作?

For some reason neither the accepted answer nor any others work for me for "Sending email in .NET through Gmail". Why would they not work?

更新:我已经尝试了所有的答案(接受和其他方面)在另一个问题,但没有一个工作。

UPDATE: I have tried all the answers (accepted and otherwise) in the other question, but none of them work.

我只想知道它是否适用于其他任何人,否则Google可能已经改变了一些事情(以前发生过)。

I would just like to know if it works for anyone else, otherwise Google may have changed something (which has happened before).

当我尝试使用 SmtpDeliveryMethod.Network 的代码片段时,我很快收到发送(消息)上的SmtpException。消息是

When I try the piece of code that uses SmtpDeliveryMethod.Network, I quickly receive an SmtpException on Send(message). The message is


SMTP服务器需要安全连接或客户端未通过身份验证。

The SMTP server requires a secure connection or the client was not authenticated.

服务器响应是:


5.5.1认证需要。了解更多信息,请参阅< - 严重,结束于此。

5.5.1 Authentication Required. Learn more at" <-- seriously, it ends there.

更新:

这是一个很久以前提出的问题,而接受的答案是我在不同的项目中使用了很多次的代码。

This is a question that I asked a long time ago, and the accepted answer is code that I've used many, many times on different projects.

我在这篇文章和其他EmailSender项目中采取了一些想法,以创建一个 EmailSender项目在Codeplex ,它是为可测试性设计的,并支持我最喜欢的SMTP服务,如GoDaddy和Gmail。

I've taken some of the ideas in this post and other EmailSender projects to create an EmailSender project at Codeplex. It's designed for testability and supports my favourite SMTP services such as GoDaddy and Gmail.

推荐答案

CVertex,确保检查您的代码,如果没有显示任何内容,请发布,我只是在一个测试ASP.NET网站上启用此工作,并且它的工作。

CVertex, make sure to review your code, and, if that doesn't reveal anything, post it. I was just enabling this on a test ASP.NET site I was working on, and it works.

实际上,在某些时候,我的代码有一个问题,直到我在控制台程序中有一个更简单的版本,并没有发现它,并且看到它正在运行(Gmail边没有改变,重新担心)。以下代码的工作方式与您提到的示例一样:

Actually, at some point I had an issue on my code. I didn't spot it until I had a simpler version on a console program and saw it was working (no change on the Gmail side as you were worried about). The below code works just like the samples you referred to:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
                EnableSsl = true
            };
            client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
            Console.WriteLine("Sent");
            Console.ReadLine();
        }
    }
}

我也使用web.config, http://msdn.microsoft.com/en- us / library / w355a94k.aspx 和代码(因为配置文件中没有匹配的 EnableSsl 。)

I also got it working using a combination of web.config, http://msdn.microsoft.com/en-us/library/w355a94k.aspx and code (because there is no matching EnableSsl in the configuration file :( ).

这篇关于使用C#通过Gmail SMTP服务器发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆