step1: create asp.net webservice project
step2 :add related ddl on mentioned using library
step3 : view webservice on browser and check it's working or not
step4 : if working copy bin folder to sharepoint 80(port-maybe change) bin folder
step5 :copy asmx file to layouts folder or ISAP folder and mapping to check the service
step 6 : finally call the webservice URL via script to send email in this way you can send external mail also like gmail. (http://win-2016/server/_layouts/15//OnPremisMailService/MailService.asmx)
function SendMailService(hostName, portNumber, fromEmail, dispName, toEmail, cc, bcc, subject, body) {
var data = {
hostName: hostName,
portNumber: portNumber,
fromEmail: fromEmail,
dispName:dispName,
toEmail: toEmail,
cc: cc,
bcc: bcc,
subject: subject,
body: body
};
data = JSON.stringify(data);
$.ajax({
type: "POST",
url: _spPageContextInfo.webAbsoluteUrl +"/_layouts/15/OnPremisMailService/MailService.asmx/SendEmail",
//data: "{ hostName: '" + hostName + "',portNumber: " + portNumber + ",fromEmail: '" + fromEmail + "',dispName: '" + dispName + "',toEmail: '" + toEmail + "',cc: '" + cc + "',bcc: '" + bcc + "', subject: '" + subject + "', body: '" + body + "' }",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r.d);
},
error: function (r) {
console.log(r.responseText);
},
failure: function (r) {
console.log(r.responseText);
}
});
return false;
}
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Services;
namespace OnPremisMailService
{
/// <summary>
/// Summary description for MailService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class MailService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string SendEmail(string hostName, int portNumber, string fromEmail,string dispName, string toEmail, string cc, string bcc, string subject, string body)
{
if (SendMailMessage(hostName, portNumber,fromEmail, dispName, toEmail, cc, bcc, subject, body))
{
return "Email sent.";
}
else
{
return "Email failed to send";
}
}
[WebMethod]
public string SendGmail(string hostName, int portNumber, bool enableSSL, string password, string fromEmail, string dispName, string toEmail, string cc, string bcc, string subject, string body)
{
if (SendGmailMailMessage(hostName, portNumber, enableSSL, password,fromEmail, dispName, toEmail, cc, bcc, subject, body))
{
return "Email sent.";
}
else
{
return "Email failed to send";
}
}
#region Members
/// <summary>
/// SendMailMessage
/// </summary>
/// <param name="toEmail"></param>
/// <param name="fromEmail"></param>
/// <param name="bcc"></param>
/// <param name="cc"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
public bool SendMailMessage(string hostName, int portNumber,string fromEmail,string dispName, string toEmail, string cc, string bcc, string subject, string body)
{
try
{
//create the MailMessage object
MailMessage mMailMessage = new MailMessage();
//set the sender address of the mail message
if (!string.IsNullOrEmpty(fromEmail))
{
mMailMessage.From = new MailAddress(fromEmail, dispName);
}
if (!string.IsNullOrEmpty(toEmail))
{
if (toEmail.Contains("|"))
{
string[] toList = toEmail.Split('|');
foreach (var m in toList)
{
//set the recipient address of the mail message
mMailMessage.To.Add(new MailAddress(m));
}
}
else
{
//set the recipient address of the mail message
mMailMessage.To.Add(new MailAddress(toEmail));
}
}
//set the blind carbon copy address
if (!string.IsNullOrEmpty(bcc))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
//set the carbon copy address
if (!string.IsNullOrEmpty(cc))
{
if (cc.Contains("|"))
{
string[] cclist = cc.Split('|');
foreach (var m in cclist)
{
//set the recipient address of the mail message
mMailMessage.CC.Add(new MailAddress(m));
}
}
else
{
//set the recipient address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
}
}
//set the subject of the mail message
if (!string.IsNullOrEmpty(subject))
{
mMailMessage.Subject = subject;
}
else
{
mMailMessage.Subject = "OnPremisConnect";
}
//set the body of the mail message
mMailMessage.Body = body;
//set the format of the mail message body
mMailMessage.IsBodyHtml = true;
//set the priority
mMailMessage.Priority = MailPriority.Normal;
//create the SmtpClient instance
using (SmtpClient mSmtpClient = new SmtpClient(hostName, portNumber))
{
mSmtpClient.Port = portNumber;
mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.Host = hostName;// "npkmail.OnPremisnet.OnPremis.gov.sg";
//send the mail message
mSmtpClient.Send(mMailMessage);
return true;
}
}
catch
{
return false;
}
}
/// <summary>
/// SendMailMessage
/// </summary>
/// <param name="toEmail"></param>
/// <param name="fromEmail"></param>
/// <param name="bcc"></param>
/// <param name="cc"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
public bool SendGmailMailMessage(string hostName, int portNumber,bool enableSSL, string password, string fromEmail, string dispName, string toEmail, string cc, string bcc, string subject, string body)
{
try
{
//create the MailMessage object
MailMessage mMailMessage = new MailMessage();
//set the sender address of the mail message
if (!string.IsNullOrEmpty(fromEmail))
{
mMailMessage.From = new MailAddress(fromEmail, dispName);
}
if (!string.IsNullOrEmpty(toEmail))
{
if (toEmail.Contains("|"))
{
string[] toList = toEmail.Split('|');
foreach (var m in toList)
{
//set the recipient address of the mail message
mMailMessage.To.Add(new MailAddress(m));
}
}
else
{
//set the recipient address of the mail message
mMailMessage.To.Add(new MailAddress(toEmail));
}
}
//set the blind carbon copy address
if (!string.IsNullOrEmpty(bcc))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
//set the carbon copy address
if (!string.IsNullOrEmpty(cc))
{
if (cc.Contains("|"))
{
string[] cclist = cc.Split('|');
foreach (var m in cclist)
{
//set the recipient address of the mail message
mMailMessage.CC.Add(new MailAddress(m));
}
}
else
{
//set the recipient address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
}
}
//set the subject of the mail message
if (!string.IsNullOrEmpty(subject))
{
mMailMessage.Subject = subject;
}
else
{
mMailMessage.Subject = "OnPremisConnect";
}
//set the body of the mail message
mMailMessage.Body = body;
//set the format of the mail message body
mMailMessage.IsBodyHtml = true;
//set the priority
mMailMessage.Priority = MailPriority.Normal;
//create the SmtpClient instance
using (SmtpClient smtp = new SmtpClient(hostName, portNumber))
{
smtp.Host = hostName;
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(fromEmail, password);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = portNumber;
smtp.Send(mMailMessage);
}
return true;
}
catch(Exception ex)
{
return false;
}
}
/// <summary>
/// Determines whether an email address is valid.
/// </summary>
/// <param name="emailAddress">The email address to validate.</param>
/// <returns>
/// <c>true</c> if the email address is valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsValidEmailAddress(string emailAddress)
{
// An empty or null string is not valid
if (String.IsNullOrEmpty(emailAddress))
{
return (false);
}
// Regular expression to match valid email address
string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
// Match the email address using a regular expression
Regex re = new Regex(emailRegex);
if (re.IsMatch(emailAddress))
return (true);
else
return (false);
}
#endregion
}
}
No comments:
Post a Comment