在vb.net中发送带附件的电子邮件 [英] Send email with attachment in vb.net
问题描述
我正在使用vb.net在我的程序中创建一个带附件的电子邮件发件人。在发送过程中无法读取组合框文本框中的SMPT Server PORT,SSL,HOST和IsBodyHtml值。我仍然是这种语言的新手,任何人都可以帮助我使用我的代码。?!这是我的节目的屏幕截图 https://fbcdn-sphotos-ca.akamaihd.net /hphotos-ak-prn2/1450048_755648697783890_582817513_n.jpg [ ^ ]。
Im creating an email sender with attachment in my program using vb.net. SMPT Server PORT, SSL, HOST and IsBodyHtml values in the comboboxes|textboxes cannot be read during the sending process. Im still new in this language, can anyone help me with my codes.?! Here's my screen shot of the program https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn2/1450048_755648697783890_582817513_n.jpg[^] .
Imports System.Net.Mail
Public Class sendmail
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim attachment As System.Net.Mail.Attachment
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential((txtuser.Text), (txtpass.Text))
'sending failure
Smtp_Server.Port = txtserport.Text 'cannot be read
Smtp_Server.EnableSsl = cboserenssl.Text 'cannot be read
Smtp_Server.Host = cboserhost.Text 'cannot be read
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtfrom.Text)
e_mail.To.Add(txtto.Text)
e_mail.Subject = txtsubject.Text
e_mail.IsBodyHtml = cbohtmlbody.Text 'cannot be read
e_mail.IsBodyHtml = cbohtmlbody.Text
e_mail.Body = txtbody.Text
'this line here excute correctly but if a user didd'nt attach a file, sending fails..
'i want to send even w/o an attach file..
attachment = New System.Net.Mail.Attachment(txtattach.Text) 'file path
e_mail.Attachments.Add(attachment) 'attachment
Smtp_Server.Send(e_mail)
txtmailstat.Text = "Successfully send" ' if it fails to send, how to?
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
推荐答案
试试这个修复 -
Try this fix-
attachment = New System.Net.Mail.Attachment(txtattach.Text) 'file path
if(attachment <> null)
e_mail.Attachments.Add(attachment) 'attachment
编辑 -
您正在为布尔属性分配Text值。所以首先将文本转换为Boolen
e_mail.IsBodyHtml = cbohtmlbody.Text'无法读取
应该是
e_mail.IsBodyHtml = Convert.ToBoolean(cbohtmlbody.Text)'无法读取
同样适用于其他错误..
同样
Smtp_Server.Host = cboserhost.Text'无法读取
应该是
Smtp_Server.Host = cboserhost.SelectedItem.Text'无法读取
如果不学习基础知识就不要跳入编程
Edit-
You are assigning a Text value to the Boolean property. So convert the text to Boolen first
e_mail.IsBodyHtml = cbohtmlbody.Text 'cannot be read
should be
e_mail.IsBodyHtml = Convert.ToBoolean(cbohtmlbody.Text) 'cannot be read
Same for other errors too..
Similarly
Smtp_Server.Host = cboserhost.Text 'cannot be read
Should be
Smtp_Server.Host = cboserhost.SelectedItem.Text 'cannot be read
And do not jump into programming without learning the basics
这篇关于在vb.net中发送带附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!