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

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

问题描述

出于某种原因,对于,无论是接受的答案还是其他任何答案都不适合我通过 Gmail 在 .NET 中发送电子邮件".为什么它们不起作用?

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 的一段代码时,我很快在 Send(message) 上收到一个 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 项目中的一些想法,在 Codeplex 创建了一个 EmailSender 项目.它专为可测试性而设计,并支持我最喜欢的 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 :( ).

默认情况下,您还需要为安全性较低的应用"启用访问权限.在您的 Gmail 设置页面中:google.com/settings/security/lesssecureapps.如果您收到异常服务器响应为:5.5.1 需要身份验证",则这是必要的.– 感谢@Ravendarksky

By default you will also need to enable access for "less secure apps" in your gmail settings page: google.com/settings/security/lesssecureapps. This is necessary if you are getting the exception "`The server response was: 5.5.1 Authentication Required. – thanks to @Ravendarksky

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

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