Wysyłanie wiadomości e-mail in.NET przez Gmail
Zamiast polegać na moim hosta do wysyłania wiadomości e-mail, myślałem o wysyłaniu wiadomości e-mail za pomocą mojego konta Gmail. E-maile to spersonalizowane e-maile do zespołów, które gram w moim programie. Czy to możliwe?
22 answers
Upewnij się, że używasz System.Net.Mail
, a nie przestarzałego System.Web.Mail
. Robienie SSL z System.Web.Mail
jest obrzydliwym bałaganem hakerskich rozszerzeń.
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-16 09:49:27
Powyższa odpowiedź nie działa. Musisz ustawić DeliveryMethod = SmtpDeliveryMethod.Network
, inaczej pojawi się błąd " client was not authenticated". Również zawsze jest to dobry pomysł, aby umieścić limit czasu.
Poprawiony kod:
using System.Net.Mail;
using System.Net;
var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "password";
const string subject = "test";
const string body = "Hey now!!";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-08 04:16:17
Dla pozostałych odpowiedzi do pracy" z serwera " najpierw Włącz Dostęp dla mniej bezpiecznych aplikacji na koncie gmail.
Wygląda na to, że ostatnio google zmieniło politykę bezpieczeństwa. Najlepiej oceniana odpowiedź nie działa, dopóki nie zmienisz ustawień konta zgodnie z opisem tutaj: https://support.google.com/accounts/answer/6010255?hl=en-GB
W marcu 2016 r. google ponownie zmieniło lokalizację ustawienia!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-22 13:03:02
Http://www.systemnetmail.com/ jest prawdopodobnie najbardziej absurdalnie kompletną stroną poświęconą pojedynczej przestrzeni nazw.NET...ale ma wszystko, co chciałbyś wiedzieć o wysyłaniu poczty przez. Net, czy to ASP.NET lub na pulpicie.
http://www.systemwebmail.com/ był oryginalnym adresem URL w poście, ale nie powinien być używany dla.NET 2.0 i nowszych.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-17 16:11:01
Służy do wysyłania wiadomości e-mail z załączeniem.. Proste i krótkie..
Źródło: http://coding-issues.blogspot.in/2012/11/sending-email-with-attachments-from-c.html
using System.Net;
using System.Net.Mail;
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your [email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your [email protected]", "your password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-29 13:10:20
Google może blokować próby logowania z niektórych aplikacji lub urządzeń, które nie korzystają z nowoczesnych standardów bezpieczeństwa. Ponieważ te aplikacje i urządzenia są łatwiejsze do włamania, zablokowanie ich pomaga zachować bezpieczeństwo konta.
Niektóre przykłady aplikacji, które nie obsługują najnowszych standardów bezpieczeństwa to:- Aplikacja Poczta na iPhone lub iPad z systemem iOS 6 lub poniżej
- aplikacja pocztowa w telefonie Windows phone poprzedzająca wydanie 8.1
- niektóre klienty pocztowe typu Microsoft Outlook i Mozilla Thunderbird
Dlatego musisz włączyć mniej bezpieczne logowanie na swoim koncie google.
Po zalogowaniu się na konto google przejdź do:
Https://myaccount.google.com/lesssecureapps
lub
https://www.google.com/settings/security/lesssecureapps
W C# możesz użyć następującego kodu:
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("[email protected]", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-13 02:40:14
Oto moja wersja: "Send Email In C # Using Gmail ".
using System;
using System.Net;
using System.Net.Mail;
namespace SendMailViaGmail
{
class Program
{
static void Main(string[] args)
{
//Specify senders gmail address
string SendersAddress = "[email protected]";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "[email protected]";
//Specify The password of gmial account u are using to sent mail(pw of [email protected])
const string SendersPassword = "Password";
//Write the subject of ur mail
const string subject = "Testing";
//Write the contents of your mail
const string body = "Hi This Is my Mail From Gmail";
try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-28 08:43:17
Aby go uruchomić, musiałem włączyć moje konto gmail, aby umożliwić dostęp innym aplikacjom. Odbywa się to za pomocą "włącz mniej bezpieczne aplikacje" i również za pomocą tego linku: https://accounts.google.com/b/0/DisplayUnlockCaptcha
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-22 13:04:56
Mam nadzieję, że ten kod będzie działał dobrze. Możesz spróbować.
// Include this.
using System.Net.Mail;
string fromAddress = "[email protected]";
string mailPassword = "*****"; // Mail id password from where mail will be sent.
string messageBody = "Write the body of the message here.";
// Create smtp connection.
SmtpClient client = new SmtpClient();
client.Port = 587;//outgoing port for the mail.
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);
// Fill the mail form.
var send_mail = new MailMessage();
send_mail.IsBodyHtml = true;
//address from where mail will be sent.
send_mail.From = new MailAddress("[email protected]");
//address to which mail will be sent.
send_mail.To.Add(new MailAddress("[email protected]");
//subject of the mail.
send_mail.Subject = "put any subject here";
send_mail.Body = messageBody;
client.Send(send_mail);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-16 09:56:59
Include this,
using System.Net.Mail;
I wtedy,
MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt16("587");
client.Credentials = new System.Net.NetworkCredential("[email protected]","password");
client.EnableSsl = true;
client.Send(sendmsg);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-16 09:59:29
Źródło : wyślij e-mail w ASP.NET C #
Poniżej znajduje się przykładowy kod roboczy do wysyłania poczty za pomocą C#, w poniższym przykładzie używam serwera smtp google.
Kod jest dość oczywiste, zastąpić e-mail i hasło z wartości e-mail i hasło.
public void SendEmail(string address, string subject, string message)
{
string email = "[email protected]";
string password = "put-your-GMAIL-password-here";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 12:29:57
Jeśli chcesz wysłać wiadomość e-mail w tle, wykonaj poniższe czynności
public void SendEmail(string address, string subject, string message)
{
Thread threadSendMails;
threadSendMails = new Thread(delegate()
{
//Place your Code here
});
threadSendMails.IsBackground = true;
threadSendMails.Start();
}
I dodaj przestrzeń nazw
using System.Threading;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 18:03:31
Użyj tej drogi
MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt32("587");
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("[email protected]","MyPassWord");
client.Send(sendmsg);
Nie zapomnij o tym:
using System.Net;
using System.Net.Mail;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-09 06:57:43
Jedna Wskazówka! Sprawdź skrzynkę odbiorczą nadawcy, może potrzebujesz zezwolić na mniej bezpieczne aplikacje. Zobacz: https://www.google.com/settings/security/lesssecureapps
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-21 18:53:56
Spróbuj Tego,
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-31 08:23:05
Zmiana nadawcy w Gmailu / Outlook.com e-mail:
Aby zapobiec spoofing-Gmail / Outlook. com nie pozwala wysyłać z dowolnej nazwy konta użytkownika.
Jeśli masz ograniczoną liczbę nadawców, możesz postępować zgodnie z poniższymi instrukcjami, a następnie ustawić pole From
na ten adres: wysyłanie wiadomości z innego adresu
Jeśli chcesz wysłać z dowolnego adresu e-mail (takiego jak Formularz opinii na stronie internetowej, gdzie użytkownik wprowadza swój e-mail i nie chcesz e-mailem do ciebie bezpośrednio) o najlepsze co możesz zrobić to:
msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));
To pozwoli Ci po prostu nacisnąć "odpowiedz" na swoim koncie e-mail, aby odpowiedzieć fanowi Twojego zespołu na stronie opinii, ale nie dostaną twojego prawdziwego e-maila, co prawdopodobnie doprowadziłoby do tony spamu.
Jeśli znajdujesz się w kontrolowanym środowisku, działa to świetnie, ale pamiętaj, że widziałem kilka klientów poczty e-mail wysyłanych na adres from, nawet gdy podano reply-to (nie wiem, który).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-07 20:49:44
Miałem ten sam problem, ale został rozwiązany, przechodząc do ustawień bezpieczeństwa Gmaila i zezwalając na mniej bezpieczne aplikacje . Kod z Domenic & Donny działa, ale tylko jeśli włączyłeś to ustawienie
Jeśli jesteś zalogowany (do Google), możesz śledzić ten link i przełączać "Włącz" dla "Dostęp dla mniej bezpiecznych aplikacji"Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-25 07:09:18
using System;
using System.Net;
using System.Net.Mail;
namespace SendMailViaGmail
{
class Program
{
static void Main(string[] args)
{
//Specify senders gmail address
string SendersAddress = "[email protected]";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "[email protected]";
//Specify The password of gmial account u are using to sent mail(pw of [email protected])
const string SendersPassword = "Password";
//Write the subject of ur mail
const string subject = "Testing";
//Write the contents of your mail
const string body = "Hi This Is my Mail From Gmail";
try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-10 13:57:35
Oto jedna z metod wysyłania poczty i uzyskiwania danych uwierzytelniających z sieci.config:
public static string SendEmail(string To, string Subject, string Msg, bool bodyHtml = false, bool test = false, Stream AttachmentStream = null, string AttachmentType = null, string AttachmentFileName = null)
{
try
{
System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
newMsg.BodyEncoding = System.Text.Encoding.UTF8;
newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null)
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
disposition.FileName = AttachmentFileName;
disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
newMsg.Attachments.Add(attachment);
}
if (test)
{
smtpClient.PickupDirectoryLocation = "C:\\TestEmail";
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
}
else
{
//smtpClient.EnableSsl = true;
}
newMsg.IsBodyHtml = bodyHtml;
smtpClient.Send(newMsg);
return SENT_OK;
}
catch (Exception ex)
{
return "Error: " + ex.Message
+ "<br/><br/>Inner Exception: "
+ ex.InnerException;
}
}
I odpowiedniej sekcji w web.config:
<appSettings>
<add key="mailCfg" value="[email protected]"/>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network defaultCredentials="false" host="mail.exapmple.com" userName="[email protected]" password="your_password" port="25"/>
</smtp>
</mailSettings>
</system.net>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-08 06:10:55
Problem dla mnie polegał na tym, że moje hasło miało w sobie blackslash " \ " , które kopiuję wklejając nie zdając sobie sprawy, że może to powodować problemy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-15 11:34:40
Spróbuj tego
public static bool Send(string receiverEmail, string ReceiverName, string subject, string body)
{
MailMessage mailMessage = new MailMessage();
MailAddress mailAddress = new MailAddress("[email protected]", "Sender Name"); // [email protected] = input Sender Email Address
mailMessage.From = mailAddress;
mailAddress = new MailAddress(receiverEmail, ReceiverName);
mailMessage.To.Add(mailAddress);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient("smtp.gmail.com", 587)
{
EnableSsl = true,
UseDefaultCredentials = false,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("[email protected]", "pass") // [email protected] = input sender email address
//pass = sender email password
};
try
{
mailSender.Send(mailMessage);
return true;
}
catch (SmtpFailedRecipientException ex)
{ }
catch (SmtpException ex)
{ }
finally
{
mailSender = null;
mailMessage.Dispose();
}
return false;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-27 12:40:41
Kopiowanie z kolejna odpowiedź, powyższe metody działają, ale gmail zawsze zastępuje e-maile "from" I "reply to" z rzeczywistym wysyłającym kontem gmail. widocznie jest jednak praca:
Http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html
"3. Na karcie Konta kliknij link "Dodaj kolejny adres e-mail, który posiadasz", a następnie zweryfikuj GO"
Lub ewentualnie to
Update 3: Reader Derek Bennett mówi: "rozwiązaniem jest przejście do ustawień Gmaila: konta i "ustaw domyślne" konto inne niż konto gmail. Spowoduje to, że gmail ponownie zapisze Pole From bez względu na domyślny adres e-mail konta."
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-22 13:07:55