Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send email without mailbee
#1
Sends email. Uses C# and .NET instead of mailbee.

Function SendMailCs
Code:
Copy      Help
;/
function $account $to $subject $text [flags] [$headers] [$attachments] ;;flags: 1 HTML

;Sends email message.

;account - SMTP settings. See example.
;to - email.
;subject - subject.
;text - text.
;headers - XML containing additional headers. See example.
;attachments - XML containing attachments. See example.

;REMARKS
;Text encodings not tested.

;EXAMPLES
;str account=
;;<account
;;host='smtp.example.com'
;;user='me@example.com'
;;password='PASSWORD'
;;port='587'
;;ssl='true'
;;timeout='30'
;;email='sender-email-optional-if-user-is-email@example.com'
;;displayName='optional sender name'
;;/>
;SendMailCs account "friend@example.com" "Test" "Hi"
;
;str headers=
;;<headers>
;;<h name='Organization'>kkkkkkkk</h>
;;<h name='X-Custom'>mmmmmmmm</h>
;;</headers>
;str attachments=
;;<attachments>
;;<a>c:\test\image.png</a>
;;<a>c:\test\program.exe</a>
;;</attachments>
;SendMailCs account "friend@example.com" "Test2" "Hi, <b>HTML</b>" 1 headers attachments


;CsScript bug(?): strings cannot be null
if(!headers) headers=""
if(!attachments) attachments=""

CsScript x.SetOptions("references=System.XML")
x.AddCode("")
x.Call("Email.Send" account to subject text flags&1 headers attachments)


#ret
//C# code
using System;
using System.Net;
using System.Net.Mail;
using System.Xml.Linq;

public class Email {
,static public void Send(string account, string email, string subject, string body, bool isHtml, string headers, string attachments) {
,,string host=null, user=null, password=null, fromEmail=null, fromName=null;
,,int port=0, timeout=60;
,,bool ssl=false;
,,var x=XElement.Parse(account);
,,foreach(var k in x.Attributes()) {
,,,switch(k.Name.LocalName) {
,,,case "host": host=(string)k; break;
,,,case "user": user=(string)k; break;
,,,case "password": password=(string)k; break;
,,,case "email": fromEmail=(string)k; break;
,,,case "displayName": fromName=(string)k; break;
,,,case "port": port=(int)k; break;
,,,case "timeout": timeout=(int)k; break;
,,,case "ssl": ssl=(bool)k; break;
,,,}
,,}
,,
,,var fromAddress = new MailAddress(fromEmail != null ? fromEmail : user, fromName);
,,var toAddress = new MailAddress(email);

,,using(var smtp = new SmtpClient {
,,,Host = host,
,,,Port = port,
,,,EnableSsl = ssl,
,,,DeliveryMethod = SmtpDeliveryMethod.Network,
,,,UseDefaultCredentials = false,
,,,Credentials = new NetworkCredential(user, password),
,,,Timeout = timeout*1000,
,,})
,,using (var m = new MailMessage(fromAddress, toAddress) {
,,,Subject = subject,
,,,Body = body,
,,,IsBodyHtml = isHtml
,,}) {
,,,if(!string.IsNullOrEmpty(headers)) {
,,,,x=XElement.Parse(headers);
,,,,foreach(var k in x.Elements()) m.Headers.Add(k.Attribute("name").Value, k.Value);
,,,}
,,,if(!string.IsNullOrEmpty(attachments)) {
,,,,x=XElement.Parse(attachments);
,,,,foreach(var k in x.Elements())  {
,,,,,var mta=k.Attribute("mediaType");
,,,,,m.Attachments.Add(new Attachment(k.Value, mta!=null ? mta.Value : null));
,,,,}
,,,}
,,,smtp.Send(m);
,,}
,}
}


Forum Jump:


Users browsing this thread: 1 Guest(s)