通过Gmail SMTP服务器用C#发送电子邮件发送电子邮件、Gmail、SMTP

2023-09-02 01:16:24 作者:寂寞红酒

由于某些原因,接受的答案或任何其他人不为我工作的发送的电子邮件。NET。为什么它不工作?

For some reason the accepted answer or any others don't work for me for "Sending email in .NET through Gmail". Why would it not work?

更新:我已经尝试了所有的答案(接受和其他)中的其他问题,但没有工作

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

我只是想知道,如果它适用于任何人,否则谷歌已经改变了一些东西(这之前已经发生)。

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

当我尝试了一段使用code SmtpDeliveryMethod.Network ,我很快收到发送(消息)一SmtpException。该消息是

When I try the piece of code that uses SmtpDeliveryMethod.Network, I quickly receive a 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.

更新:

这是一个问题,我问了很久以前,公认的答案已经code,我在不同的项目中使用很多次。

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

我已经采取了一些想法在这个岗位等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,请一定要检查你的code和,如果不透露任何东西,它张贴。我只是让这对一个测试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.

事实上,在某些时候,我在我的code有一个问题。我没有发现它,直到我有一个控制台程序简化版本,看到它的工作(在Gmail的侧面没有变化,你担心)。下面code工作就像你提到的样本:

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和code(因为配置文件:(在没有匹配的 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 :( ).

 
精彩推荐
图片推荐