billb59

Hey all, the below vb.net code is returning nothing for mHost. What I need is the users mail settings... something like "mail.my.com" returning as mHost. Does anyone have working source / ideas how it's done

Dim mMailSettings As System.Net.Configuration.MailSettingsSectionGroup

Dim mHost As String = mMailSettings.Smtp.Network.Host.ToString 'NEED TO GET "mail.my.com"

'

Debug.Write(vbCrLf)

Debug.Write(mHost)

Debug.Write(vbCrLf)

'

Dim client As New SmtpClient(mHost ) '("mail.my.com")

'

' Credentials are necessary if the server requires the client

' to authenticate before it will send e-mail on the client's behalf.

client.UseDefaultCredentials = True

client.Send(message)

Thanks,

billb




Re: Windows Forms General MailSettingsSectionGroup question?

billb59

Hey all, the below vb.net code is returning nothing for mHost. What I need is the users mail settings... something like "mail.my.com" returning as mHost. Does anyone have working source / ideas how it's done

Dim mMailSettings As System.Net.Configuration.MailSettingsSectionGroup

Dim mHost As String = mMailSettings.Smtp.Network.Host.ToString 'NEED TO GET "mail.my.com"

'

Debug.Write(vbCrLf)

Debug.Write(mHost) 'Should be my local machines email account - "mail.my.com"

Debug.Write(vbCrLf)

'

Dim client As New SmtpClient(mHost ) '("mail.my.com")

'

' Credentials are necessary if the server requires the client

' to authenticate before it will send e-mail on the client's behalf.

client.UseDefaultCredentials = True

client.Send(message)

Thanks,

billb






Re: Windows Forms General MailSettingsSectionGroup question?

Yu Guo – MSFT

Hi, billb59,

Based on my understanding, you cannot get smtp host in your application, can you

In fact, MailSettingsSectionGroup is a class that respond to mailSettings Element (Network Settings) configuration in your app config file or machine.config file.

http://msdn2.microsoft.com/en-us/library/system.net.configuration.mailsettingssectiongroup.aspx

And in the config file, you should write like

Code Snippet

< xml version="1.0" encoding="utf-8" >

<configuration>

<configSections>

<!-- settings-->

</configSections>

<userSettings>

<!-- settings-->

</userSettings>

<system.net>

<mailSettings>

<smtp deliveryMethod="Network">

<network

host="localhost"

port="25"

defaultCredentials="true"

/>

</smtp>

</mailSettings>

</system.net>

</configuration>

http://msdn2.microsoft.com/en-us/library/system.net.configuration.mailsettingssectiongroup.aspx

Hope this helps,

Regards






Re: Windows Forms General MailSettingsSectionGroup question?

billb59

Hi Yu,

I was not able to get the above working in the app config... the only way it seems to work is when I specify the SMTPClient default host.... hmmm...

Thanks,

billb






Re: Windows Forms General MailSettingsSectionGroup question?

Yu Guo – MSFT

Hi, billb59,

I tried to set the Configuration in my app.config, and it works.

You can try the following codes.

c#

Code Block

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

MailSettingsSectionGroup mailSettings = NetSectionGroup.GetSectionGroup(config).MailSettings;

MessageBox.Show(mailSettings.Smtp.Network.Host);

VB

Code Block

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MailSettingsSectionGroup mailSettings = NetSectionGroup.GetSectionGroup(config).MailSettings;

MessageBox.Show(mailSettings.Smtp.Network.Host);

Hope this helps,

Regards