Send mail smtp
Here is an example of how to send an email using SMTP (Simple Mail Transfer Protocol) in various programming languages:
Python
import smtplib
# Set up the SMTP server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('[email protected]', 'your_password')
# Set up the email message
msg = 'Subject: Test email\n\nThis is a test email sent using Python.'
msg = msg.encode('utf-8')
# Send the email
server.sendmail('[email protected]', '[email protected]', msg)
server.quit()
Java
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) {
// Set up the SMTP server
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "your_password");
}
});
// Set up the email message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("Test email");
message.setText("This is a test email sent using Java.");
// Send the email
Transport.send(message);
}
}
C#
using System.Net.Mail;
public class SendEmail {
public static void Main(string[] args) {
// Set up the SMTP server
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("[email protected]", "your_password");
// Set up the email message
MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress("[email protected]"));
message.Subject = "Test email";
message.Body = "This is a test email sent using C#.";
// Send the email
client.Send(message);
}
}
Node.js
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // or 'STARTTLS'
auth: {
user: '[email protected]',
pass: 'your_password'
}
});
let mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Test email',
text: 'This is a test email sent using Node.js.'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Email sent: ' + info.response);
});
Make sure to replace [email protected]
and your_password
with your actual Gmail address and password. Also, make sure to enable less secure apps in your Google account settings if you're using Gmail.
Note: These examples are just a starting point, and you may need to modify them to fit your specific use case. Additionally, be aware of any email sending limits and best practices when sending emails programmatically.