Αντί να βασίζομαι στον οικοδεσπότη μου για να στέλνω email, σκεφτόμουν να στέλνω τα μηνύματα ηλεκτρονικού ταχυδρομείου χρησιμοποιώντας το λογαριασμό μου στο Gmail. Τα μηνύματα ηλεκτρονικού ταχυδρομείου είναι εξατομικευμένα μηνύματα ηλεκτρονικού ταχυδρομείου προς τις μπάντες που παίζω στην εκπομπή μου. Είναι δυνατόν να γίνει αυτό;
Βεβαιωθείτε ότι χρησιμοποιείτε το System.Net.Mail
και όχι το απαρχαιωμένο System.Web.Mail
. Η χρήση SSL με το System.Web.Mail
είναι ένα μεγάλο χάος από δύσκολες επεκτάσεις.
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);
}
Η παραπάνω απάντηση δεν λειτουργεί. Πρέπει να ορίσετε DeliveryMethod = SmtpDeliveryMethod.Network
αλλιώς θα επιστρέψει με το σφάλμα "client was not authenticated". Επίσης, είναι πάντα καλή ιδέα να βάζετε ένα χρονικό όριο.
Αναθεωρημένος κώδικας:
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);
}
Εδώ είναι η δική μου έκδοση: "Αποστολή email σε C # χρησιμοποιώντας το 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();
}
}
}
}