swgdesign
asked on
web.config mutliple smtp accounts
I have a web application that is accessable via 2 different domains and I need to send out emails using 2 different smtp accounts because of the 2 domains. This is to avoid emails being marked as unsafe, phishing or spam due to being sent from an address that doesn't match the mailserver.
Is there a way to do this even though the web.config SMTP settings area will only allow 1 account. IF so, how?
Can I send the emails some other way rather than using the settings stored in web.config?
IF so, how?
THanks
Simon
Is there a way to do this even though the web.config SMTP settings area will only allow 1 account. IF so, how?
Can I send the emails some other way rather than using the settings stored in web.config?
IF so, how?
THanks
Simon
ASKER
I did see this code but I don't understand the accepted answer or how to implement it, it seems bitty?
I understand that you can declare a new declaration and name it and then setup a new section using this declaration...
BUT what I don't understand is this part and how it is used in sending an email
At the moment I use a function in a class that accepts the following parameters;
I understand that you can declare a new declaration and name it and then setup a new section using this declaration...
<configSections>
<section name="smtp_3" type="System.Net.Configuration.SmtpSection"/>
</configSections>
<mailSettings>
<smtp_1 deliveryMethod="Network" from="mail1@temp.uri">
<network host="..." defaultCredentials="false"/>
</smtp_1>
</mailsettings
BUT what I don't understand is this part and how it is used in sending an email
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_1");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_2");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_3");
At the moment I use a function in a class that accepts the following parameters;
Public Shared Sub SendMailMessage(ByVal Sender As String, ByVal Recipient As String, ByVal BCC As String, ByVal CC As String, ByVal Subject As String, ByVal Body As String)
I would store the values inside appSettings node
http://stackoverflow.com/questions/4363038/setting-multiple-smtp-settings-in-web-configYes, That solution is confusing to implement.
instead of storing SMTP details in web.config file, do you agree to store SMTP details in a XML file and read the data dynamically in your mailing process?
This will allow you to store multiple SMTP details and retrieve while sending a mail (using dynamic SMTP details). if so i can provide some more details with sample code.
ASKER
I am open to either using appSettings or an XML file.
Can either fo you give me an example of what needs to be setup coded to implement such a solution?
POints increased :)
Can either fo you give me an example of what needs to be setup coded to implement such a solution?
POints increased :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That look complete and exactly what I could do with. Give me a day or so and I'll get back to you if it works :)
ASKER
Quick question...
At the moment 'SendMailMessage' is within a class called EmailHelper in a file called email.vb. I call SendEmailMessage in my code by using EmailHelper.SendMailMessag e().
Can I just insert your code within my EmailHelper class?
At the moment 'SendMailMessage' is within a class called EmailHelper in a file called email.vb. I call SendEmailMessage in my code by using EmailHelper.SendMailMessag
Can I just insert your code within my EmailHelper class?
Imports System.Web
Imports System.Xml
Imports System.Net.Mail
Public Class EmailHelper
Public Shared Sub SendMailMessage(ByVal Sender As String, ByVal Recipient As String, ByVal BCC As String, ByVal CC As String, ByVal Subject As String, ByVal Body As String)
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(Sender)
mMailMessage.To.Add(New MailAddress(Recipient))
If Not String.IsNullOrEmpty(BCC) Then
mMailMessage.Bcc.Add(New MailAddress(BCC))
End If
If Not String.IsNullOrEmpty(CC) Then
mMailMessage.CC.Add(New MailAddress(CC))
End If
mMailMessage.Subject = Subject
mMailMessage.Body = Body
mMailMessage.IsBodyHtml = True
mMailMessage.Priority = MailPriority.Normal
Dim mSmtpClient As New SmtpClient()
mSmtpClient.Send(mMailMessage)
End Sub
End Class
Yes,
you can add my sample in a class file (EmailHelper.vb) and use it.
you can add my sample in a class file (EmailHelper.vb) and use it.
Dim emailObj As New EmailHelper
emailObj.SendMailMessage("smtp1", "test1@test1.com", "test11@test1.com", "", "", "Subject 1", "MsgBody 1")
'emailObj.SendMailMessage("smtp2", "test2@test1.com", "test22@test1.com", "", "", "Subject 2", "MsgBody 2")
'emailObj.SendMailMessage("smtp3", "test3@test1.com", "test33@test1.com", "", "", "Subject 3", "MsgBody 3")
Below is the EmailHelper class as you need and I HAVE UPDATED ONE LINE IN THE CODE.
' EmailHelper.vb
' Updated by Rose :-
'
' To read the __SecureSmtpConfig.xml file, i used Server.MapPath("__SecureSmtpConfig.xml")
' but inside the class we need to do call the file by the below line of code
' System.Web.HttpContext.Current.Server.MapPath("__SecureSmtpConfig.xml")
'
Imports System.Xml
Imports System.Net.Mail
Public Class EmailHelper
#Region "Dynamic SMTP"
Public Sub SendMailMessage(ByVal _SMTPNAME As String, ByVal Sender As String, ByVal Recipient As String, ByVal BCC As String, ByVal CC As String, ByVal Subject As String, ByVal Body As String)
Dim message As New MailMessage(Sender, Recipient)
message.Subject = "Using the new SMTP client." & Subject
message.Body = "Using this new feature, you can send an e-mail message from an application very easily." & Environment.NewLine & Body
Dim SmtpDet As New _SMTP_Det
SmtpDet = GetSMTPDet(_SMTPNAME)
' host, port, and credentials.
Dim client As New SmtpClient()
client.Host = SmtpDet.HostName
client.Port = SmtpDet.Port
client.Credentials = New System.Net.NetworkCredential(SmtpDet.UserName, SmtpDet.Password)
Try
client.Send(message)
Catch ex As Exception
Dim exMsg = ex.Message
' Or
'Throw ex
End Try
End Sub
Public Class _SMTP_Det
Public HostName As String = ""
Public Port As String = ""
Public UserName As String = ""
Public Password As String = ""
End Class
Public Function GetSMTPDet(ByVal _SMTPNAME As String) As _SMTP_Det
Dim retSmtp As New _SMTP_Det
Dim xmlDoc As New XmlDocument
'xmlDoc.Load(Server.MapPath("__SecureSmtpConfig.xml")) ' I modified this line
xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("__SecureSmtpConfig.xml"))
Dim root As XmlElement = xmlDoc.DocumentElement
Dim smtpName As String = ""
For Each elem As XmlElement In root.GetElementsByTagName("smtp")
smtpName = elem.GetAttribute("name")
If smtpName = _SMTPNAME Then
retSmtp.HostName = elem.GetElementsByTagName("host").Item(0).InnerText
retSmtp.Port = elem.GetElementsByTagName("port").Item(0).InnerText
retSmtp.UserName = elem.GetElementsByTagName("username").Item(0).InnerText
retSmtp.Password = elem.GetElementsByTagName("password").Item(0).InnerText
Exit For
End If
Next
Return retSmtp
End Function
#End Region
End Class
check the below link
http://stackoverflow.com/questions/4363038/setting-multiple-smtp-settings-in-web-config
Hope it may help you