How to send email with PowerShell

Following code snippet really works for me: $Username = “MyUserName”; $Password = “MyPassword”; $path = “C:\attachment.txt”; function Send-ToEmail([string]$email, [string]$attachmentpath){ $message = new-object Net.Mail.MailMessage; $message.From = “YourName@gmail.com”; $message.To.Add($email); $message.Subject = “subject text here…”; $message.Body = “body text here…”; $attachment = New-Object Net.Mail.Attachment($attachmentpath); $message.Attachments.Add($attachment); $smtp = new-object Net.Mail.SmtpClient(“smtp.gmail.com”, “587”); $smtp.EnableSSL = $true; $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); … Read more

How to configure SMTP settings in web.config

By setting the values <mailSettings> section of the in the web.config you can just new up an SmtpClient and the client will use those settings. https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.-ctor?view=net-6.0#system-net-mail-smtpclient-ctor Web.Config file: <configuration> <system.net> <mailSettings> <smtp from=”yourmail@gmail.com”> <network host=”smtp.gmail.com” port=”587″ userName=”yourmail@gmail.com” password=”yourpassword” enableSsl=”true”/> </smtp> </mailSettings> </system.net> </configuration> C#: SmtpClient smtpClient = new SmtpClient(); smtpClient.Send(msgMail); However, if authentication is needed, … Read more

Sending email fails when two factor authentication is on for Gmail

Create a custom app in you Gmail security settings. Log-in into Gmail with your account Navigate to https://security.google.com/settings/security/apppasswords In ‘select app’ choose ‘custom’, give it an arbitrary name and press generate It will give you 16 chars token. Use the token as password in combination with your full Gmail account and two factor authentication will … Read more

Sending mail error with python smtplib

The following works for microsoft, google, yahoo accounts on Python 2.7 and Python 3.2: #!/usr/bin/env python # -*- coding: utf-8 -*- “””Send email via smtp_host.””” import smtplib from email.mime.text import MIMEText from email.header import Header ####smtp_host=”smtp.live.com” # microsoft ####smtp_host=”smtp.gmail.com” # google smtp_host=”smtp.mail.yahoo.com” # yahoo login, password = … recipients_emails = [login] msg = MIMEText(‘body…’, ‘plain’, … Read more

How to send email to multiple address using System.Net.Mail

MailMessage msg = new MailMessage(); msg.Body = ….; msg.To.Add(…); msg.To.Add(…); SmtpClient smtp = new SmtpClient(); smtp.Send(msg); To is a MailAddressCollection, so you can add how many addresses you need. If you need a display name, try this: MailAddress to = new MailAddress( String.Format(“{0} <{1}>”,display_name, address));

Python smtplib proxy support

Use SocksiPy: import smtplib import socks #’proxy_port’ should be an integer #’PROXY_TYPE_SOCKS4′ can be replaced to HTTP or PROXY_TYPE_SOCKS5 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, proxy_host, proxy_port) socks.wrapmodule(smtplib) smtp = smtplib.SMTP() …