crax15 Posted May 8, 2013 at 09:16 PM Report #506319 Posted May 8, 2013 at 09:16 PM Tenho o seguinte codigo para enviar um email a varios contactos que tenho numa base de dados: Dim mMailMessage As New MailMessage() Dim dados As New Data.DataSet Dim adaptador As New SqlDataAdapter("Select E_Mail from Utilizadores", ligacao) ' Set the sender address of the mail message mMailMessage.From = New MailAddress("meu_email@gmail.com") ' Set the subject of the mail message mMailMessage.Subject = "Assunto" ' Set the body of the mail message mMailMessage.Body = "test" ' Secify the format of the body as HTML mMailMessage.IsBodyHtml = True ' Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal Dim n_email As Integer adaptador.Fill(dados, "Emails") ligacao.Open() comando.Connection = ligacao comando.CommandText = "SELECT count(*) AS maximo FROM Utilizadores" comando.ExecuteScalar() n_email = comando.ExecuteScalar ligacao.Close() For i = 1 To n_email ' Set the recepient address of the mail message mMailMessage.To.Add(New MailAddress(dados.Tables("Emails").Rows(i).Item(0))) ' Instantiate a new instance of SmtpClient Dim mSmtpClient As New SmtpClient() ' Send the mail message mSmtpClient.Send(mMailMessage) Next Aparente mente parece que esta tudo bem, so que a conecçao exede o tempo e dá erro, nao sei o porque, alguem me poderia dar uma ajudinha? Obrigado! Pedro'O
nelsonr Posted May 8, 2013 at 11:20 PM Report #506336 Posted May 8, 2013 at 11:20 PM hmmmm, falta definir o servidor de SMTP mSmtpClient.SmtpServer = "....." E ja agora... For i = 1 To n_email ' Set the recepient address of the mail message mMailMessage.To.Add(New MailAddress(dados.Tables("Emails").Rows(i).Item(0))) ' Instantiate a new instance of SmtpClient Dim mSmtpClient As New SmtpClient() ' Send the mail message mSmtpClient.Send(mMailMessage) Next Em cada ciclo repetes a lista anterior e acrescentas mais um email. O mMailMessage.To tem de ser limpo antes de adicionares a conta de email, senão fica com os emails anteriores mais o atual.
crax15 Posted May 9, 2013 at 02:04 PM Author Report #506418 Posted May 9, 2013 at 02:04 PM (edited) eu o servidor de SMTP tenho.o definido no webconfig assim: <system.net> <mailSettings> <smtp from="*******@gmail.com"> <network host="smtp.gmail.com" port="465" userName="*******@gmail.com" password="******"/> </smtp> </mailSettings> </system.net> Edited May 9, 2013 at 02:27 PM by crax15 Pedro'O
nelsonr Posted May 9, 2013 at 02:17 PM Report #506424 Posted May 9, 2013 at 02:17 PM Devias censurar esses dados. E se enviares um só email funciona? Já verificaste a situação de estar a adicionar emails ao To sem limpar em cada ciclo?
crax15 Posted May 9, 2013 at 03:08 PM Author Report #506434 Posted May 9, 2013 at 03:08 PM Para limpar seria assim nao? For i = 1 To n_email mMailMessage.To.Clear() ' Set the recepient address of the mail message mMailMessage.To.Add(New MailAddress(dados.Tables("Emails").Rows(i).Item(0))) ' Instantiate a new instance of SmtpClient Dim mSmtpClient As New SmtpClient() ' Send the mail message mSmtpClient.Send(mMailMessage) Next Pedro'O
Leudassdf Posted May 12, 2013 at 08:23 AM Report #506954 Posted May 12, 2013 at 08:23 AM Boas, Eu tambem tive problemas ao tenter enviar emails pela porta 465. Eu para resolver o problema coloquei a porta 587. Basicamente a 465 e uma porta com SSL e a 487 é com TLS. Espero ter ajudado. Já agora aparece-te algum erro? O que acontece? Cumprimentos
crax15 Posted May 12, 2013 at 11:59 AM Author Report #506970 Posted May 12, 2013 at 11:59 AM Nao fiz dessa maneira, arranjei este codigo e funcionou, agora um problema que tive foi com alguns emails que dava erro, penso que seria por utilizar caracteres especiais no endereço de email, de resto esta a funcionar perfeitamente, até agora nao me tem dado problemas. Dim cliente As New SmtpClient Dim remetente As New MailAddress("mail@gmail.com") Dim mensagem As New MailMessage 'mensagem.To.Add(destinatário) For i = 0 To n_email - 1 ' Set the recepient address of the mail message mensagem.Bcc.Add(New MailAddress(dados.Tables("Emails").Rows(i).Item(0))) Next mensagem.From = remetente mensagem.Subject = "Temos um novo filme para si!" mensagem.Body = "corpo" mensagem.IsBodyHtml = True cliente.Host = "smtp.gmail.com" cliente.Port = 587 Dim credenciais As New System.Net.NetworkCredential credenciais.UserName = "username" credenciais.Password = pass cliente.EnableSsl = True cliente.UseDefaultCredentials = False cliente.Credentials = credenciais Try cliente.Send(mensagem) MsgBox("Envio", MsgBoxStyle.Information, "") Catch ex As Exception MsgBox("Erro", MsgBoxStyle.Critical, "Falha de comunicação") End Try Pedro'O
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now